Capybara & rspec

I cannot get Capybara to work successfully, it complains that has_text is an undefined method.

I created a new rails 3.1 project ( rails new test -T ).

Gemfile:

 source 'http://rubygems.org' gem 'rails', '3.1.3' gem 'sqlite3' group :assets do gem 'sass-rails', '~> 3.1.5' gem 'coffee-rails', '~> 3.1.1' gem 'uglifier', '>= 1.0.3' end gem 'jquery-rails' group :test do gem 'rspec-rails' gem 'capybara' end 

I installed the specs folder: rails g rspec:install .

specifications / spec_helper.rb:

 ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false end 

And finally, my test file:

 require 'spec_helper' feature "the test" do scenario "GET /" do visit('/') page.should have_text('Welcome aboard') end end 

So, I run rspec: bundle exec rspec spec/my_test.rb , and this is an error:

 F Failures: 1) the test GET / Failure/Error: page.should have_text('Welcome aboard') NoMethodError: undefined method `has_text?' for #<Capybara::Session> # ./spec/my_test.rb:6:in `block (2 levels) in <top (required)>' 
+4
source share
1 answer

Most likely you are using capybara 1.1.2 , which is the current stable version, but it does not have a has_text? method has_text? . Can you either use has_content? (and the corresponding have_content ), or use capybara directly from the github repository, as Skydreamer suggested.

Note that has_content? has a slightly different behavior, as described in README . On the other hand, using gem directly from the repository is not always safe, as this version may not be very stable.

+5
source

Source: https://habr.com/ru/post/1388733/


All Articles