2012-10-06 153 views
0

我正在研究hartl的教程。目前第9章。 当我运行测试,我得到这个错误:Michael Hartl Ruby on rails教程第9章 - 测试失败

c:\rails_project\sample_app>bundle exec rspec spec/ 

Failures: 

1) Authentication authorization for non-signed-in users when attempting to visit a 
protected page after signing in should render the desired protected page 
Failure/Error: visit edit_user_path(user)NameError:undefined local variable or 
method `requests' for #<UsersController:0x37cff38> 
# ./app/helpers/sessions_helper.rb:36:in `store_location' 
# ./app/controllers/users_controller.rb:44:in `signed_in_user' 
# ./spec/requests/authentication_pages_spec.rb:57:in `block (5 levels) 
in <top (required)>' 

2) Authentication authorization in the Users controller visiting the edit page 
Failure/Error: before { visit edit_user_path(user) } 
NameError:undefined local variable or method `requests' for 
#<UsersController:0x439d430> 
# ./app/helpers/sessions_helper.rb:36:in `store_location' 
# ./app/controllers/users_controller.rb:44:in `signed_in_user' 
# ./spec/requests/authentication_pages_spec.rb:75:in `block (5 levels) 
in <top (required)>' 

3) Authentication authorization in the Users controller Submitting to the update 
action Failure/Error: before { put user_path(user) } NameError: undefined local 
variable or method `requests' for #<UsersController: 0x44f78d0> 
# ./app/helpers/sessions_helper.rb:36:in `store_location' 
# ./app/controllers/users_controller.rb:44:in `signed_in_user' 
# ./spec/requests/authentication_pages_spec.rb:80:in `block (5 levels) 
in <top (required)>' 

Finished in 9.33 seconds 
70 examples, 3 failures, 3 pending 

我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 another page" do 
      before { click_link "Home"} 
      it { should 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('Settings', href: edit_user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 

    describe "followed by signout" do 
     before { click_link "Sign out" } 
     it { should have_link('Sign in') } 
    end 
    end 
end 

describe "authorization" do 

    describe "for non-signed-in users" do 
    let(:user) { FactoryGirl.create(:user) } 

    describe "when attempting to visit a protected page" do 
     before do 
     visit edit_user_path(user) 
     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
     end 

     describe "after signing in" do 

     it "should render the desired protected page" do 
      page.should have_selector('title', text: 'Edit user') 
     end 
     end 
    end 
    end 

    describe "in the Users controller" do 

    describe "visiting the edit page" do 
     before { visit edit_user_path(user) } 
     it { should have_selector('title', text: "Sign in") } 
    end 

    describe "Submitting to the update action" do 
     before { put user_path(user) } 
     specify { response.should redirect_to(signin_path) } 
    end 
    end 


    describe "as wrong user" do 
    let(:user) { FactoryGirl.create(:user) } 
    let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") } 
    before { sign_in user } 

    describe "visiting User#edit page" do 
     before { visit edit_user_path(wrong_user) } 
     it { should_not have_selector('title', text: full_title('Edit user')) } 
    end 

    describe "submitting a PUT request to the User#update action" do 
     before { put user_path(wrong_user) } 
     specify { response.should redirect_to(root_path) } 
    end 
    end  
end 
end 

我发现这个问题是类似于一个显示在Michael Hartl's Ruby on Rails Tutorial. Failed test in Chapter 9 但这种解决方案并不在我的情况下工作。任何人有想法?

+0

你需要添加代码UsersController –

回答

2

也许只是在虚空中出手,但是您在SessionsHelper.store_location中存在拼写错误。将“请求”更改为“请求”。

相关问题