undefined method `visit' when using RSpec and Capybara in rails

后端 未结 5 943
面向向阳花
面向向阳花 2020-12-02 09:20

I can\'t get capybara working with rspec. It gives me this error:

undefined method `visit\' for #

        
相关标签:
5条回答
  • 2020-12-02 10:04

    I also had this problem,

    Adding require 'rails_helper' at the top of my feature ended up fixing my problem:

    require 'rails_helper'
    
    RSpec.describe "Products", type: :request do
     describe "GET /products" do
     it "display tasks" do
      Product.create!(:name => "samsung")
      visit products_path
      page.should have_content("samsung")
      #expect(response).to have_http_status(200)
      end
     end
    end
    

    And add the 'config.include Capybara::DSL' in rails_helper.rb

    RSpec.configure do |config|
    
     config.fixture_path = "#{::Rails.root}/spec/fixtures"
    
     config.use_transactional_fixtures = true
    
     config.infer_spec_type_from_file_location!
    
     config.include Capybara::DSL
    
    end
    
    0 讨论(0)
  • 2020-12-02 10:07

    Since Capybara 2.0 one has to use folder spec/features Capybara commands don't work in folder spec/requests anymore.

    0 讨论(0)
  • 2020-12-02 10:08

    Try to add:

      config.include Capybara::DSL
    

    to your config block.

    # This file is copied to spec/ when you run 'rails generate rspec:install'
    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'rspec/autorun'
    
    # Requires supporting ruby files with custom matchers and macros, etc,
    # in spec/support/ and its subdirectories.
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
    
    RSpec.configure do |config|
      # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
      # config.fixture_path = "#{::Rails.root}/spec/fixtures"
      config.use_transactional_fixtures = true
      config.infer_base_class_for_anonymous_controllers = false
      config.order = "random"
      # Include path helpers
      config.include Rails.application.routes.url_helpers
    
      config.include Capybara::DSL
    
    end
    
    0 讨论(0)
  • 2020-12-02 10:13

    Adding require 'rails_helper' at the top of my feature ended up fixing my problem:

    require 'rails_helper'
    
    describe "security", :type => :feature do
    
      it "signs users in" do
        visit new_sessions_path
        fill_in "username", :with => "user"
        fill_in "password", :with => "pass"
        click_button "Sign In"
    
        page.should have_content('Login Successful')
      end
    end
    

    This seems odd to me since every example I've seen for rspec and capybara didn't have that require, but oh well. Problem solved.

    Original Answer (older versions of rspec)

    require 'spec_helper' is used by older versions of RSpec. The better answer would be require 'rails_helper'.

    0 讨论(0)
  • 2020-12-02 10:20

    Try performing all your setup in a before block:

    spec/features/security_spec.rb

    describe "security" do
      before do
        visit "/sessions/new"
        fill_in "username", :with => "user"
        fill_in "password", :with => "pass"
        click_button "Sign In"
      end
    
      it "signs users in" do
        page.should have_content('Login Successful')
      end
    end
    
    0 讨论(0)
提交回复
热议问题