Kaminari and Capybara

It seems I have some kind of conflict between the capybara page method and the Kaminari page method.

What I guessed, anyway, here is the error:

Failure/Error: before { sign_in_as user } ActionView::Template::Error: wrong number of arguments (1 for 0) # ./app/models/feed.rb:9:in `microposts' [Rest of the backtrace] 

Code example:

 class Feed def microposts(opts = { urgent: false }) urgent = opts[:urgent] p Microposts.where(id: 1).page # <Capybara::Session> p Microposts.where(id: 1).page(1) # Error end end 

If I remove the pagination, the test works fine.

I don’t understand how this is possible, I think Capybara is adding the "page" method to the Object area, but since Kaminari is adding its page method to ActiveRecord :: Base (if I remember correctly), it should override Capybara.

I have not seen anyone have such problems, how is this possible?

Thanks.

+4
source share
2 answers

I had the same problem with Capybara 2.x

My function specifications are in the spec/feature directory . I understood from reading the Capybara documentation that there is no need to include Capybara::DSL in your spec_helper if you use a function directory. It is already included.

There is a warning if you include Capybara::DSL in spec_helper that it will pollute the global namespace, which is why this is a bad idea!

Learn more about this rspec-rails page on Capybara.

+3
source

This is a bit hacky, but I was able to get around the problem (where Capybara "pollutes" the space of objects) by abandoning the method in my specification:

 # Capybara adds a 'page' method to the Object class which conflicts with the Kaminari scope # Remove it here to allow things to work Object.send :undef_method, :page 

I traced where this happens and essentially:

  • The #page method comes from Capybara :: DSL
  • The Capybara :: DSL method is included in the Object class through the RSpe # configure.include method (see lib / capybara / rspec.rb).
  • RSpec then includes it in a β€œgroup,” however I believe that this is where it falls into Object.

The solution here can only be to change the method name in Capybara, but I think this is not the solution I'm ready to make :)

+2
source

All Articles