问题描述
我正在使用Ruby on Rails 3.2.2和rspec-rails-2.8.1.为了使我的规格文件 DRY (不要重复自己)并播种test
数据库,我想为所有那些规格文件运行before(:each)
钩子.也就是说,在我所有的规格文件中,我都有以下代码:
I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. In order to make my spec files DRY (Don't Repeat Yourself) and to seed the test
database I would like to run a before(:each)
hook for all those spec files. That is, in all my spec files I have the following code:
describe 'test description' do
before(:each) do
load "#{Rails.root}/db/seeds.rb"
end
...
end
是否可以添加before(:each)
钩子的某处",以便所有spec文件都可以运行它? 您有何建议?
Is it possible to add "somewhere" that before(:each)
hook so that all spec files can run it? What do you advice?
推荐答案
在spec_helper.rb
中:
RSpec.configure do |config|
#your other config
config.before(:each) do
#your code here
end
end
有很多可用的配置.例如:config.before(:each, :type => [:routing, :controller, :request])
There is much configuration available. For instance: config.before(:each, :type => [:routing, :controller, :request])
您甚至可以创建自己的标签并将代码与之关联:
You can even create your own tags and associate code to it:
config.around :each, unobstrusive: true do |example|
Capybara.current_driver = :rack_test
example.run
Capybara.current_driver = :selenium
end
这篇关于是否可以在“某处"添加“一个`before(:each)`钩子,以便所有spec文件都可以运行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!