Capybara & RSpec

こ雲淡風輕ζ 提交于 2019-12-23 13:03:23

问题


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)>'

回答1:


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.



来源:https://stackoverflow.com/questions/8688925/capybara-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!