Capybara: undefined method 'visit'

后端 未结 6 2099
广开言路
广开言路 2020-12-13 00:25

When running my specs with rspec & capybara, it can\'t find capybara\'s visit method. Is there another initialization step I need to do?

$bundle exec rsp         


        
相关标签:
6条回答
  • 2020-12-13 01:00

    Because RSpec.configure not including capybara DSL in spec_helper.rb

    It is an ugly solution, but you can add this to your spec_helper.rb.

    module ::RSpec::Core
      class ExampleGroup
        include Capybara::DSL
        include Capybara::RSpecMatchers
      end
    end 
    

    The git issue for this:

    https://github.com/rspec/rspec-rails/issues/503

    0 讨论(0)
  • 2020-12-13 01:00

    Unfortunately this workaround doesn't do it for me. I still get

    NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>
    

    The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter You need to look at the branch '28817499-subscribe'

    edit: If i put include Capybara::DSL inside my describe block it works.

    but including Capybara::DSL in the global scope is not recommended!

    Because I do not know a good way.

    0 讨论(0)
  • 2020-12-13 01:01

    A few things to note here :

    1. The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
    2. load Capybara dsl explicitly inside your spec_helper

    
           require 'capybara/rails'
           require 'capybara/rspec'
           include Capybara::DSL
    

    0 讨论(0)
  • 2020-12-13 01:05

    Just ran into this issue myself.

    So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.

    For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.

    They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q

    Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.

    This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814

    0 讨论(0)
  • 2020-12-13 01:08

    For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".

    Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.

    0 讨论(0)
  • 2020-12-13 01:13

    This worked for me.

    require 'rspec/rails'
    require 'rspec/autorun'
    require 'capybara/rspec'
    require 'capybara/rails'
    
    RSpec.configure do |config|
      config.include Capybara::DSL, :type => :request
    end
    

    This enables you to use Capybara's helpers inside spec/requests.

    0 讨论(0)
提交回复
热议问题