RSpec.configure do |config|
config.before(:suite) do
cleaner.strategy = :truncation
cleaner.clean_with(:truncation)
end
config.around(:each) do |example|
cleaner.cleaning do
example.run
end
end
end
其中一项测试:
require 'rspec'
describe 'Test of all Listing parameters' do
before(:all) do
populate_from_yaml('spec/sample_data/listing_param_filtering_data.yaml')
end
it 'filter by active listing' do
params = make_params(userId: 'users-1', limit: 100)
listings = request_shuffler(params)
expect(listings).not_to include('listing-inactive')
expect(listings).to include('listing-active-all-empty')
end
it 'filter by doorman' do
params = make_params(userId: 'users-1', limit: 100, doorman: true)
listings = request_shuffler(params)
expect(listings).to match_array(['listing-param-doorman'])
end
# and so on
end
当我运行测试指定测试名称时,一切正常
rspec spec/test_spec.rb
但如果我执行所有测试:
rspec spec
我收到了错误:
Test of all Listing parameters filter by active listing
Failure/Error: populate_from_yaml('spec/sample_data/listing_param_filtering_data.yaml')
Sequel::UniqueConstraintViolation:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "agents_pkey"
DETAIL: Key (id)=(agent-1) already exists.
Debug data ...
Test of all Listing parameters filter by doorman
Failure/Error: populate_from_yaml('spec/sample_data/listing_param_filtering_data.yaml')
Sequel::UniqueConstraintViolation:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "agents_pkey"
DETAIL: Key (id)=(agent-1) already exists.
Debug data ...
...
看起来rspec只是忽略了config.around(:each)停止清理数据库,忽略之前(:all)挂钩并试图填充数据库每个例子......有什么想法吗?
我正在使用 红宝石2.2.0p0 rspec的-3.2.0
顺便说一句,这不是铁路
答案 0 :(得分:1)
before(:all)
是邪恶的,并且是许多头痛的根源,因此许多rspec多次考虑to remove the command at all。此块未包含在事务中,因此测试后不会回滚数据。您应该手动清除after(:all)
块中的数据。
我个人的最佳做法是仅使用before(:all)
设置环境变量,全局库配置......但绝不使用它来命中数据库。
我建议您更改before(:all)
的{{1}}。