1

かなり複雑な一連のテストがあり、以下のはるかに単純な形式で再現されています。

# 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)ですか?

4

1 に答える 1

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

これら2つのケースをテストする別の方法を見つけます。そのように 2 つの数字を入力する必要はありますか?

于 2014-12-10T14:27:28.210 に答える