为什么我不建议“在全球范围内包括Capybara :: DSL!”

时间:2013-09-04 14:33:46

标签: ruby-on-rails ruby rspec capybara

每次运行规范时,即使规范通过,例如

$ rspec spec/integration/view_homepage_spec.rb 
including Capybara::DSL in the global scope is not recommended!
.

Finished in 0.6174 seconds
1 example, 0 failures

Randomized with seed 14130

$ 

我的Gemfile有:

group :test, :development do
  gem 'rspec-rails'
  gem 'capybara'
end

我的spec_helper有:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara'
include Capybara::DSL
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

如果我发表评论include Capybara::DSL,那么像visit这样的水豚方法就无效了。

1 个答案:

答案 0 :(得分:18)

因为不建议在全局范围内包含Capybara::DSL

这包括系统中全局的许多方法,这些方法可能会干扰您自己的代码。

这是正确的方法:

RSpec.configure do |config|
  config.include Capybara::DSL, :type => :feature
end