Capybara NoMethodError: undefined method `visit

2019-04-14 04:23发布

问题:

Thank for response.
I getting error in application Git application with the error

Failures:

  1) Page pages page creation with invalid information should not create a page
     Failure/Error: before(:each) { visit new_admin_page_path }
     NoMethodError:
       undefined method `visit' for #<Page:0x00000005094bb0>
     # ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) Page pages page creation with invalid information error messages 
     Failure/Error: before(:each) { visit new_admin_page_path }
     NoMethodError:
       undefined method `visit' for #<Page:0x000000051671f0>
     # ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'

Finished in 1.21 seconds
14 examples, 2 failures

What I've missed ?

回答1:

I cloned your repository and ran the spec. The problem is that you are using the term page for two different things, the Capybara page (subject { page }) and your model with the name Page (which you also name page). Those two names conflict, which is why rspec is complaining that there is no method visit for #<Page:0x00000005094bb0>.

To solve this just rename the page you create in the initial let:

let(:mypage) { Factory.create(:page) }

That will avoid the conflict by naming your page model mypage instead of page.

There are other issues however. Your user factory does not use a sequence for assigning emails, so you get a validation error because it says the email is already taken.

I changed your user factory to this:

sequence(:email) { |n| "abc#{n}@example.com"}
factory :user do
  email
  password "foobar"
  password_confirmation "foobar"
  ...

That fixes the validation error, but you now get another error. In the line in your first spec, you should be checking that count does not change on the class Page, not the record page (or mypage).

So change that to:

expect { click_button "Create Page" }.not_to change(Page, :count)

This will still leave you with errors, because you also do not have any create action for your PagesController. From what you wrote, it seems like the information is invalid so it should not even get to this action. I don't know exactly what you're trying to do here so I'll leave the rest for you to figure out.



回答2:

If you recently upgraded to Capybara 2.0 & your integration tests were working with previous version of Capybara, you may try renaming requests directory to features. Check http://www.codelearn.org/blog/how-to-make-old-integration-tests-work-with-capybara-2-0



回答3:

Add the following to your spec/spec_helper.rb (in your Spork.prefork block):

require 'capybara/rspec'
require 'capybara/rails'