我有一系列相当复杂的测试,下面用更简单的形式再现:
# Credit cards that should succeed; totally opening to other ways of running this loop, if it's what's causing the error! It's just the only thing I thought of to be DRY
["2134", "1234"].each do |card|
describe "enter card number", job: true do
before do
fill_in "card_number", with: card
end
it "should create a record" do
Record.count.should == 1
end
end
end
# Credit card that should fail
# Just the one number here
describe "enter card number" do
before do
fill_in "card_number", with: "5678"
end
it "should create a record" do
Record.count.should == 0
end
end
在配置中我需要关闭use_transactional_fixtures
,因为这些是基于javascript的测试,而事务性设备并不适合我。所以我尝试像这样实现数据库清理器(使用Sucker Punch gem指令https://github.com/brandonhilkert/sucker_punch,因为我最终还需要测试gem):
# Database cleaner set up below
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# Clean up all jobs specs with truncation
config.before(:each, job: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
问题是这没有帮助:
Record.count
继续构建Records
基本上唯一的方法就是在before(:all)
强制清理(before
和after
都没有做过)
# Credit cards that should succeed
["2134", "1234"].each do |card|
describe "enter card number", job: true do
before(:all) do
Record.destroy_all
end
before do
# did not work to put the destroy here
fill_in "card_number", with: card
end
it "should create a record" do
Record.count.should == 1
end
# did not work to put the destroy here
# after do
# Record.destroy_all
# end
end
end
# Credit card that should fail
describe "enter card number" do
before do
# On this one, the Record.destroy_all could have gone anywhere
Record.destroy_all
fill_in "card_number", with: "5678"
end
it "should create a record" do
Record.count.should == 0
end
end
如何正确设置数据库清理程序?或者我应该做before(:all)
?
答案 0 :(得分:0)
首先,您使用的是哪个版本的database_cleaner
?
如果你看一下gem的文档,你会看到不同的东西:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
此外,我不确定这是否会按预期运作:
["2134", "1234"].each do |card|
describe "enter card number", job: true do
...
end
end
我会找到一种不同的方法来测试这两种情况。是否真的有必要输入两个这样的数字?