0

我正在编写第 8 章的 RoR 教程。我正在尝试完成第 8.2.4 节 - 更改布局链接并运行$ bundle exec rspec spec/,但出现以下错误:

Failures:

  1) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     NoMethodError:
     undefined method `sign_in' for #<SessionsController:0x007fef66b85fd0>
     # ./app/controllers/sessions_controller.rb:9:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

  2) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     NoMethodError:
       undefined method `sign_in' for #<SessionsController:0x007fef66b33eb0>
     # ./app/controllers/sessions_controller.rb:9:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

  3) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     NoMethodError:
       undefined method `sign_in' for #<SessionsController:0x007fef6698bba8>
     # ./app/controllers/sessions_controller.rb:9:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

  4) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     NoMethodError:
       undefined method `sign_in' for #<SessionsController:0x007fef68286130>
     # ./app/controllers/sessions_controller.rb:9:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

Finished in 1.34 seconds
44 examples, 4 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:40 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:38 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information 

根据输出,我检查了spec/requests/authentication_pages_spec.rb,但似乎找不到错误。这就是我所拥有的:

require 'spec_helper'

describe "Authentication" do

subject { page }

describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
end

  describe "signin" do
before { visit signin_path }

describe "with invalid information" do
        before { click_button "Sign in" }

        it { should have_selector('title', text: 'Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

    describe "after visiting antoher page" do 
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') } 
    end
end

    describe "with valid information" do 
        let(:user) { FactoryGirl.create(:user) }
        before do
            fill_in "Email",    with: user.email
            fill_in "Password", with: user.password
            click_button "Sign in"
        end

        it { should have_selector('title', text: user.name) }
        it { should have_link('Profile', href: user_path(user)) }
        it { should have_link('Sign out', href: signout_path) }
        it { should_not have_link('Sign in', href: signin_path) }
    end
end
end

我似乎找不到错误。根据错误输出,我应该在哪里查看任何建议?谢谢你。

4

2 回答 2

0

检查您的 config/routes.rb 文件以确保:

match '/signin', to: 'sessions#new'

与您的会话控制器所看到的相匹配。“sessions_controller.rb”的创建方法应该有:

def create
  user = User.find_by_email(params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    sign_in user
    redirect_back_or user
  else
    flash.now[:error] = 'Invalid email/password combination'
    render 'new'
  end
end

只是我的猜测。在本教程中,我目前也在该区域附近。祝你好运,我希望这会有所帮助!

于 2013-02-18T03:32:57.877 回答
0

可能有点晚了,但无论如何......

检查 app/controllers/application_controller.rb 中是否有“include SessionsHelper”

助手默认包含在视图中,但您必须明确地将它们包含在控制器中

于 2013-08-24T23:39:11.237 回答