水豚规范 [英] Capybara & RSpec
问题描述
我无法使Capybara正常工作,它抱怨 has_text
是未定义的方法。
I can't make Capybara to work successfully, it complains that has_text
is an undefined method.
我创建了一个新的Rails 3.1项目( rails new test -T
)。
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
我已经安装了spec文件夹: rails g rspec:install
。
I have installed the spec folder: rails g rspec:install
.
spec / spec_helper.rb :
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
,这是错误:
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.1.2
,这是当前的稳定版本,但没有 has_text?
方法。您可以使用 has_content?$ c而是使用$ c>
(以及相应的 have_content
匹配器)或按照Skydreamer的建议直接从github存储库中使用capybara。
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.
请注意,是否具有内容?
的行为与 README
。另一方面,直接从存储库中使用gem并不总是安全的,因为此版本可能不太稳定。
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.
这篇关于水豚规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!