David Morales
David Morales

Reputation: 18064

Capybara & RSpec

I can't make Capybara to work successfully, it complains that has_text is an undefined method.

I have 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 have installed the spec folder: rails g rspec:install.

spec/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 launch rspec: bundle exec rspec spec/my_test.rb and this is the 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)>'

Upvotes: 4

Views: 4306

Answers (1)

KL-7
KL-7

Reputation: 47588

Most likely you're using capybara 1.1.2 that is the current stable version, but it doesn't have has_text? method. You can either use has_content? (and corresponding have_content matcher) instead or use capybara directly from github repository as Skydreamer suggested.

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

Upvotes: 5

Related Questions