Capybara & RSpec

4

我无法成功使用Capybara,它抱怨has_text是一个未定义的方法。

我创建了一个新的Rails 3.1项目(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

我已经安装了spec文件夹: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

最后是我的测试文件:

require 'spec_helper'

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

所以我启动rspec:bundle exec rspec spec/my_test.rb,这是错误信息:

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

KL-7,你在我的问题中修改了什么?我看到的和我写的完全一样。实际上,我对这两个版本进行了比较,它们是完全相同的。 - David Morales
不用担心,没什么特别的。只是在第一句话中用反引号包裹了“has_text”,这样它就更加显眼了。你可以在修订历史中看到它。 - KL-7
好的。修订历史在哪里?(只是想知道,因为我还没有找到那个选项)。 - David Morales
请查看FAQ - KL-7
1个回答

5

很可能你正在使用capybara 1.1.2,这是当前稳定版本,但它没有has_text?方法。你可以使用has_content?(和相应的have_content匹配器)代替,或者像Skydreamer建议的那样直接从github存储库中使用capybara。

请注意,has_content?的行为与README中描述的有些不同。另一方面,直接从存储库中使用gem并不总是安全的,因为该版本可能不太稳定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接