したがって、同じことをテストしていると思った2つの仕様がありますが、一方は失敗し、もう一方は合格します。定期的なスケジュールのあるアプリに取り組んでいます。ユーザーが定期的な旅行を作成すると、先に進み、指定された日ごとに新しい旅行が作成されます。失敗した最初のテストは次のとおりです。
it "makes future trips" do
expect{FactoryGirl.create(:recurring_transportation_trip)}.to change(Trip, :count).by(4)
end
recurring_transportation_tripは、after_saveコールバックを介して今後3回のトリップを行うトリップを作成します。このテストは、「カウントは4で変更されているはずですが、1で変更されました」というエラーで失敗します。
合格した別のテストは次のとおりです。
it "makes future trips" do
count = Trip.count
FactoryGirl.create(:recurring_transportation_trip)
Trip.count == count + 4
end
正しい機能がそこにあることを示しています。
最初のテストは確かに読みやすいですが、実際には私が思うことをテストしていません。誰かが理由を提供して説明できますか?
- - - -編集 - - - -
リクエストによるファクトリコードの追加:
FactoryGirl.define do
factory :recurring_transportation_trip, :class => :trip do
collection_time "09:00"
estimated_duration "60"
status "Confirmed"
mileage "30"
association :collection, :factory => :location
association :destination, :factory => :location
association :call, :factory => :recurring_call
end
end
そしてrecurring_callのために
FactoryGirl.define do
factory :recurring_call, :class => "Call" do
recurring true
recurring_start_date Date.today
recurring_end_date Date.today + 1.week
recurring_config [1, 3, 5]
end
end
-------編集2-------
Trip.count == count + 4
実際には何も主張しておらず、テストはTrip.count.should == count + 4
実際に失敗します。これを指摘してくれた@BenediktDeickeに感謝します。
-------編集3-------
結局、それは私のアプリケーションコードのエラーであり、最初から元のテストを信頼する必要がありました。ご覧くださった皆様、ありがとうございました。@boulderと@BenediktDeickeは、edit2で言及されているアサーションの欠如を指摘してくれてありがとう。