1

我正在使用 Michael Hartl Ruby on Rails 教程。目前第 10 章。当我创建代码以向用户添加微博并尝试运行测试时,我收到此错误:

  1) User pages profile page microposts
Failure/Error: it { should have_content(m1.content) }expected there to be content   
"Foo" in "Ruby on Rails Tutorial Sample  App | Person 90\n    \n    \n      sample 
app\n Home\n Help\n Sign in\n \n  \n\n   Person 90, 
person_88@example.com\n\n\t\n\t\t\t\t\n\t\t\t\tPerson 90\n\t\t\t\n\t\t
\n\t\tMicroposts (2)\n\t\t\t\n\n\n\n Rails Tutorial\n by Michael Hartl\n 
\n  About\n Contact\n Ne ws\n    \n  "
# ./spec/requests/user_pages_spec.rb:77:in `block (4 levels) in <top (
required)>'←[0m

2) User pages profile page microposts
 Failure/Error: it { should have_content(m2.content) } expected there to be content   
 "Bar" in "Ruby on Rails Tutorial Sample  App | Person 92\n \n \nsample app\n 
 Home\n Help\n Sign in\n \n  \n \n Person 92, person_90@example.com\n\n\t\n\
 t\t\t\t\n\t\t\t\tPerson 92\n\t\t\t\n\t\t\n\t\tMicroposts (2)\n\t\t\t\n\n\n
 \n Rails Tutorial\n by Michael Hartl\n \n  About\n Contact\n Ne ws\n    \n  "
 # ./spec/requests/user_pages_spec.rb:78:in `block (4 levels) in <top (required)>'

我的 user_pages_spec.rb 文件如下所示:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

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

    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

       before do
         30.times { FactoryGirl.create(:user) } 
         visit users_path
       end

       after(:all)  { User.delete_all }

       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      
     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
   end

   describe "signup page" do
     before { visit signup_path }

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

   describe "profile page" do
     # Code to make a user variable
    let(:user) { FactoryGirl.create(:user) }    
   let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
   let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

        before { visit user_path(user) }

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

      describe "microposts" do
      it { should have_content(m1.content) }
      it { should have_content(m2.content) }
      it { should have_content(user.microposts.count) }
    end    
  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: "user@example.com"
         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) { "new@example.com" }
       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

这个问题的明显结果是没有向用户添加微博。错误似乎与用户创建有关,但我真的不知道如何处理它。

修改后的错误:

1) User pages edit with valid information 
Failure/Error: fill_in "Name", with: new_name 
Capybara::ElementNotFound: cannot fill in, 
no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in' 
# ./spec/requests/user_pages_spec.rb:139:in `block (4 levels) in <top (required)>'

2) User pages edit with valid information
Failure/Error:←[0m ←[31mfill_in "Name", with: new_name
Capybara::ElementNotFound: cannot fill in, no text field, text area or password field 
with id, name, or label 'Name' found 
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:139:in `block (4 levels) in <top (required)>'

3) User pages edit with valid information
Failure/Error: fill_in "Name", with: new_name Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name,
or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:139:in `block (4 levels) in <top (required)>'

4) User pages edit with valid information
Failure/Error: fill_in "Name", with: new_name Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name,
or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:139:in `block (4 levels) in <top (required)>'

5) User pages edit with valid information
Failure/Error: fill_in "Name", with: new_name Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id,name, 
or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:139:in `block (4 levels) in <top (required)>'

6) User pages edit page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails')}
expected link "change" to return something
# ./spec/requests/user_pages_spec.rb:124:in `block (4 levels) in <top (required)>'

7) User pages edit page
Failure/Error: it { should have_selector('h1', text: "Update your profile") }
expected css "h1" with text "Update your profile" to return something
# ./spec/requests/user_pages_spec.rb:122:in `block (4 levels) in <top (required)>'

8) User pages edit page
Failure/Error: it { should have_selector('title', text: "Edit user") }
expected css "title" with text "Edit user" to return something
# ./spec/requests/user_pages_spec.rb:123:in `block (4 levels) in <top (required)>'

9) User pages edit with invalid information
Failure/Error: before { click_button "Save changes" }Capybara::ElementNotFound:
no button with value or id or text 'Save changes' found
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:130:in `block (4 levels) in <top (required)>'

10) User pages signup with valid information should create a user
Failure/Error: expect { click_button submit }.to change(User,  :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/requests/user_pages_spec.rb:104:in `block (4 levels) in <top (required)>'

11) User pages profile page
Failure/Error: it { should have_selector('h1', :text=> user.name) }
expected css "h1" with text "Person 26" to return something
# ./spec/requests/user_pages_spec.rb:73:in `block (3 levels) in <top ( required)>'

12) User pages profile page
Failure/Error: it { should have_selector('title', :text=> user.name) }
expected css "title" with text "Person 27" to return something
# ./spec/requests/user_pages_spec.rb:74:in `block (3 levels) in <top (required)>'

13) User pages profile page microposts
Failure/Error: it { should have_content(m1.content) }
expected there to be content "Foo" in "Internal Server Error\nCouldn't find User with 
id=2\nWEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at 127.0.0.1:59157"
# ./spec/requests/user_pages_spec.rb:77:in `block (4 levels) in <top (required)>'

14) User pages profile page microposts
Failure/Error: it { should have_content(m2.content) }
expected there to be content "Bar" in "Internal Server Error\nCouldn't find User with 
id=2\nWEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at 127.0.0.1:59157"
# ./spec/requests/user_pages_spec.rb:78:in `block (4 levels) in <top (required)>'

15) User pages index
Failure/Error: it { should have_selector('title',  text: 'All users') }
expected css "title" with text "All users" to return something
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

16) User pages index
Failure/Error: it { should have_selector('h1', text: 'All users') }
expected css "h1" with text "All users" to return something
# ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'


17) User pages index pagination
Failure/Error:it { should have_selector('div.pagination') }
expected css "div.pagination" to return something
# ./spec/requests/user_pages_spec.rb:28:in `block (4 levels) in <top (required)>'

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

19) User pages index 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:49:in `block (5 levels) in <top (equired)>'

20) User pages index delete links as an admin user should be able to delete an  
other 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:51:in `block (6 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:51:in `block (5 levels) in <top (required)>'
4

3 回答 3

0

我遇到了同样的问题——微博在浏览器中渲染得很好,但是同样的测试失败了。原来问题是我有一个错字UsersController#show- 我是通过params[:id]而不是params[:page]to @user.microposts.paginate

通过将我的代码与 GitHub 上的源代码进行比较,我发现了问题所在。

于 2013-05-11T18:59:36.297 回答
0

我也被这个困住了。当我比较 Github 上的参考代码中的规范时,我注意到我没有使用let!(参见user_pages_spec.rb的第 80 行)。

这在上一节 (10.1) 中提到过,其中let延迟加载其对象。

于 2013-03-23T05:11:50.777 回答
0

尝试更改describe "User pages" dodescribe "User pages", :js => true do. 这解决了你的问题吗?

于 2012-10-10T18:59:01.430 回答