Kaminari and Capybara conflict

戏子无情 提交于 2019-12-06 05:13:33

问题


I seem to have some sort of conflict between the page method of capybara and the page method of Kaminari.

That's 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]

The code sample :

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 guess Capybara is adding the "page" method to the Object scope, but as Kaminari add its page method to ActiveRecord::Base (if I recall correctly) it should override Capybara's one.

I did not see anyone having this kind of trouble, how is it possible ?

Thanks.


回答1:


I had the same problem with Capybara 2.x

My feature specs are in the spec/feature directory. I realised from reading the Capybara documentation that there is no need to include the Capybara::DSL in your spec_helper if your using the features directory. It's already included.

There is a warning given if you include Capybara::DSL in the spec_helper that it will pollute the global namespace and this is exactly why it's a bad idea!

Check out this rspec-rails page on Capybara for details




回答2:


This is a little bit of a hack but I was able to work around the problem (where Capybara 'pollutes' the object space) by undef-ing the method in my spec:

# 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 have traced back where this is happening and essentially:

  1. The #page method comes from Capybara::DSL
  2. The Capybara::DSL method is included into the Object class via RSpec's #configure.include method (see lib/capybara/rspec.rb).
  3. RSpec then includes it into the 'group', however I believe this is where it drops into Object.

The solution here might just be to change the name of the method in Capybara, but I guess thats not a decision I'm willing to make :)



来源:https://stackoverflow.com/questions/11848688/kaminari-and-capybara-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!