“Capybara :: ElementNotFound:没有带有值或id的按钮或文本'跟随'找到”在Hartl第11章

时间:2013-08-14 19:16:56

标签: ruby-on-rails ruby testing rspec capybara

我得到了这些Capybara错误(它们几乎都是一样的)并且不知道我做错了什么。

我的规格是:

Ruby版本:1.9.2p320

Rails版本:3.2.13

Rspec:2.11.1

计算机:Macbook Pro OS X Mountain Lion

错误

8) User pages profile page follow/unfollow buttons following a user should increment the followed user count
     Failure/Error: click_button "Follow"
     Capybara::ElementNotFound:
       no button with value or id or text 'Follow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:101:in `block (6 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:100:in `block (5 levels) in <top (required)>'

  9) User pages profile page follow/unfollow buttons following a user should increment the other user's followers count
     Failure/Error: click_button "Follow"
     Capybara::ElementNotFound:
       no button with value or id or text 'Follow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:107:in `block (6 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:106:in `block (5 levels) in <top (required)>'

  10) User pages profile page follow/unfollow buttons following a user toggling the button 
     Failure/Error: before { click_button "Follow" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Follow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:112:in `block (6 levels) in <top (required)>'

  11) User pages profile page follow/unfollow buttons unfollowing a user should decrement the followed user count
     Failure/Error: click_button "Unfollow"
     Capybara::ElementNotFound:
       no button with value or id or text 'Unfollow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:125:in `block (6 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:124:in `block (5 levels) in <top (required)>'

  12) User pages profile page follow/unfollow buttons unfollowing a user should decrement the other user's followers count
     Failure/Error: click_button "Unfollow"
     Capybara::ElementNotFound:
       no button with value or id or text 'Unfollow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:131:in `block (6 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:130:in `block (5 levels) in <top (required)>'

  13) User pages profile page follow/unfollow buttons unfollowing a user toggling the button 
     Failure/Error: before { click_button "Unfollow" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Unfollow' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:136:in `block (6 levels) in <top (required)>'

user_pages_spec.rb

describe "follow/unfollow buttons" do
      let(:other_user) { FactoryGirl.create(:user) }
      before { sign_in user }

      describe "following a user" do
        before { visit user_path(other_user) }

        it "should increment the followed user count" do
          expect do
            click_button "Follow"
          end.to change(user.followed_users, :count).by(1)
        end

        it "should increment the other user's followers count" do
          expect do
            click_button "Follow"
          end.to change(other_user.followers, :count).by(1)
        end

        describe "toggling the button" do
          before { click_button "Follow" }
          it { should have_selector('input', value: 'Unfollow') }
        end
      end

      describe "unfollowing a user" do
        before do
          user.follow!(other_user)
          visit user_path(other_user)
        end

        it "should decrement the followed user count" do
          expect do
            click_button "Unfollow"
          end.to change(user.followed_users, :count).by(-1)
        end

        it "should decrement the other user's followers count" do
          expect do
            click_button "Unfollow"
          end.to change(other_user.followers, :count).by(-1)
        end

        describe "toggling the button" do
          before { click_button "Unfollow" }
          it { should have_selector('input', value: 'Follow') }
        end
      end
    end

1 个答案:

答案 0 :(得分:1)

当错误消息报告时,您没有带有文本或ID“关注”的按钮。我已经看到这种情况发生在一个假装成按钮的锚确实存在标题为“Follow”的情况下,但Capybara希望看到一个实际的按钮那个文字。