かなり複雑な一連のテストがあり、以下のはるかに単純な形式で再現されています。
# 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ベースのテストであり、トランザクションフィクスチャが機能していなかったため、構成でオフにする必要がありました。そこで、次のようにデータベース クリーナーを実装しようとしました (最終的には gem もテストする必要があるため、 Sucker Punch gem の指示https://github.com/brandonhilkert/sucker_punchを使用):
# 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)
ですか?