2012-08-16 160 views
1

嗨,我是Rails的新手。 Ruby on Rails Tutorial第9章,Rspec faild。鐵軌版本是3.2.6。該來源是github。我有3次失敗的測試來完成本章。Michael Hartl Ruby on Rails tuttorial chapter9 Rspec失敗

終端返回此:

$ bundle exec rspec spec/requests/user_pages_spec.rb 
    ......F.FF................. 

Failures: 

    1) User pages index pagination should list each user 
    Failure/Error: page.should have_selector('li', text: user.name) 
     expected css "li" with text "Person 41" to return something 
    # ./spec/requests/user_pages_spec.rb:44:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/user_pages_spec.rb:43:in `each' 
    # ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in <top (required)>' 

    2) User pages delete links as an admin user 
    Failure/Error: it { should have_link('delete', href: user_path(User.first)) } 
     expected link "delete" to return something 
    # ./spec/requests/user_pages_spec.rb:61:in `block (4 levels) in <top (required)>' 

    3) User pages delete links as an admin user should be able to delete another user 
    Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1) 
    Capybara::ElementNotFound: 
     no link with title, id or text 'delete' found 
    # (eval):2:in `click_link' 
    # ./spec/requests/user_pages_spec.rb:63:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/user_pages_spec.rb:63:in `block (4 levels) in <top (required)>' 

Finished in 4.6 seconds 
27 examples, 3 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:42 # User pages index pagination should list each user 
rspec ./spec/requests/user_pages_spec.rb:61 # User pages delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:62 # User pages delete links as an admin user should be able to delete another user 

規格文件是這樣的:

$ cat spec/requests/user_pages_spec.rb 
require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "index" do 

    let(:user) { FactoryGirl.create(:user) } 

    before(:all) { 30.times { FactoryGirl.create(:user) } } 
    after(:all) { User.delete_all } 

    before do 
     sign_in user 
     visit users_path 
    end 

    it { should have_selector('title', text: 'All users') } 
    it { should have_selector('h1', text: 'All users') } 

    describe "pagination" do 

     it { should have_selector('div.pagination') } 

     it "should list each user" do 
     User.paginate(page: 1).each do |user| 
      page.should have_selector('li', text: user.name) 
     end 
     end 

    before do 
     sign_in FactoryGirl.create(:user) 
     FactoryGirl.create(:user, name: "Bob", email: "[email protected]") 
     FactoryGirl.create(:user, name: "Ben", email: "[email protected]") 
     visit users_path 
    end 

    it { should have_selector('title', text: 'All users') } 
    it { should have_selector('h1', text: 'All users') } 

    it "should list each user" do 
     User.all.each do |user| 
     page.should have_selector('li', text: user.name) 
     end 
    end 
    end 
end 

    describe "delete links" do 

     it { should_not have_link('delete') } 

     describe "as an admin user" do 
     let(:admin) { FactoryGirl.create(:admin) } 
     before do 
      sign_in admin 
      visit users_path 
     end 

     it { should have_link('delete', href: user_path(User.first)) } 
     it "should be able to delete another user" do 
      expect { click_link('delete') }.to change(User, :count).by(-1) 
     end 
     it { should_not have_link('delete', href: user_path(admin)) } 
     end 
    end 

    describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit user_path(user) } 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 
    end 

    describe "signup page" do 
    before { visit signup_path } 

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

    describe "signup" do 

    before { visit signup_path } 

    let(:submit) { "Create my account" } 

    describe "with invalid information" do 
     it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
     end 
    end 

    describe "with valid information" do 
     before do 
     fill_in "Name",   with: "Example User" 
     fill_in "Email",  with: "[email protected]" 
     fill_in "Password",  with: "foobar" 
     fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
     expect { click_button submit }.to change(User, :count).by(1) 
     end 

     describe "after saving the user" do 
     before { click_button submit } 
     it { should have_link('Sign out') } 
     end 
    end 
    end 

    describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
     sign_in user 
     visit edit_user_path(user) 
    end 
    describe "page" do 
     it { should have_selector('h1', text: "Update your profile") } 
     it { should have_selector('title', text: "Edit user") } 
     it { should have_link('change', href: 'http://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
     before { click_button "Save changes" } 

     it { should have_content('error') } 
    end 

    describe "with valid information" do 
     let(:new_name) { "New Name" } 
     let(:new_email) { "[email protected]" } 
     before do 
     fill_in "Name",    with: new_name 
     fill_in "Email",   with: new_email 
     fill_in "Password",   with: user.password 
     fill_in "Confirm Password", with: user.password 
     click_button "Save changes" 
     end 

     it { should have_selector('title', text: new_name) } 
     it { should have_selector('div.alert.alert-success') } 
     it { should have_link('Sign out', href: signout_path) } 
     specify { user.reload.name.should == new_name } 
     specify { user.reload.email.should == new_email } 
    end 
    end 
end 

回答

0

後期實物,但或許這能夠幫助別人爲我的幫助。

我的回答here

我想這解決了2和3失敗。

Regards

相關問題