[Ruby] [Rails] How to solve the error undefined method `visit’ when using Capybara in Rspec

less than 1 minute read

Development environment

・Ruby: 2.5.7 Rails: 5.2.4 ・Rspec-rails: 4.0.1 ・Capybara: 3.32.2 ・Vagrant: 2.2.7 ・VirtualBox: 6.1 ・OS: macOS Catalina

Cause

Capybara could not be loaded.

Solution

1. Load with require

spec_helper.rb


require'capybara/rspec' # Added
RSpec.configure do |config|
end

2. If solution method 1 does not solve the problem, force to load with DSL

spec_helper.rb


require'capybara/rspec'
RSpec.configure do |config|
   config.include Capybara::DSL # Added
end