简体   繁体   English

Michael Hartl的Rails教程第8.1章Rspec错误

[英]Michael Hartl's Rails Tutorial Chapter 8.1 Rspec Errors

First and foremost I appreciate you all taking the time to review my issue. 首先,我非常感谢大家抽出宝贵的时间来审查我的问题。

I'm a rails rookie working my way through Michael Hartl's Rails Tutorial and I've come about the following error messages when running my Rspec. 我是一个正在通过Michael Hartl的Rails教程工作的Rails新手,在运行Rspec时遇到了以下错误消息。

1) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  2) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  3) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  4) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

Finished in 0.71396 seconds
42 examples, 4 failures

I'm aware the the error message implies that I am missing app/views/sessions/create.html.erb however its supposed to be looking in app/views/sessions/new.html.erb as stated by the tutorial. 知道该错误消息表示我缺少app/views/sessions/create.html.erb但是如教程所述,它应该在app/views/sessions/new.html.erb查找。

Whenever I go ahead and create a app/views/sessions/create.html.erb the following error messages are returned. 每当我继续创建app/views/sessions/create.html.erb ,都会返回以下错误消息。

Failures: 失败:

1) Authentication with valid information 
     Failure/Error: it { should have_link('Profile',     href: user_path(user), visible: false) }
       expected #has_link?("Profile", {:href=>"/users/1", :visible=>false}) to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:27:in `block (3 levels) in <top (required)>'

  2) Authentication with valid information 
     Failure/Error: it { should have_link('Sign out',    href: signout_path) }
       expected #has_link?("Sign out", {:href=>"/signout"}) to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:28:in `block (3 levels) in <top (required)>'

  3) Authentication with valid information 
     Failure/Error: it { should have_title(user.name) }
       expected #has_title?("John Smith") to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:26:in `block (3 levels) in <top (required)>'

Below are copies of my code. 以下是我的代码的副本。 _ __ _ __ _ __ _ __ _ __ _ __ _ _ __ _ __ _ __ _ __ _ __ _ __ _ _

Rspec Test Rspec测试

require 'spec_helper'

describe "Authentication" do
  subject { page }
  describe "signin page" do
    before { visit signin_path }
    it { should have_content('Sign in') }
    it { should have_title('Sign in') }
  end
  describe "signin" do
      before { visit signin_path }
      describe "with invalid information" do
        before { click_button "Sign in" }
        it { should have_title('Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }
      end
  end
   describe "with valid information" do
          let(:user) { FactoryGirl.create(:user) }
          before do
            visit signin_path
            fill_in "Email",    with: user.email.upcase
            fill_in "Password", with: user.password
            click_button "Sign in"
          end
          it { should have_title(user.name) }
          it { should have_link('Profile',     href: user_path(user), visible: false) }
          it { should have_link('Sign out',    href: signout_path) }
          it { should_not have_link('Sign in', href: signin_path) }
    end
end

Sessions Controller 会话控制器

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])

    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

app/views/session/ new.html.erb app / views / session / new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

routes.rb routes.rb

SampleApp::Application.routes.draw do
  resources :users 
  resources :sessions,      only: [:new, :create, :destroy]
  root to: 'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signout', to: 'sessions#destroy',     via: 'delete'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root to: 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

It is looking in new.html.erb , but when it gets to the f.submit in that view, another request is generated and it executes the create method in your sessions controller. 它正在new.html.erb 查找,但是当它到达该视图中的f.submit时,将生成另一个请求,并在您的会话控制器中执行create方法。 However, you don't have any code for the if branch (ie when the user exists), so it takes the default action of trying to render a view by the name of create . 但是,您没有if分支的任何代码(即,当用户存在时),因此它将采取默认操作,尝试使用create的名称呈现视图。

If you check carefully, I suspect you'll find that you've not implemented some of the code that the tutorial calls for you to implement at the point you are in the tutorial. 如果仔细检查,可能会发现您尚未实现本教程中要求您实现的一些代码。

Make sure controller has the if statement fulfilled --> app/controller/users_controller.rb 确保控制器已满足if语句-> app / controller / users_controller.rb

def create
  @user = User.new(user_params)

  if @user.save
    redirect_to @user    #This statement is important
  else
    render 'new'
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM