私はRuby 2.2.1とRails 4.2.0を使用していますオブジェクトを再利用するRspecテスト
モジュールをカバーするためにテストケースを追加します。モジュールは、基本的に、別のシステムから取り込まれたデータに対していくつかのQAチェックを実行します。私が抱えている問題は、テストケース全体で、モジュール内の反復が個々のテストケースのために個々のオブジェクトの代わりに同じオブジェクトを再使用していることです。
サンプルテストケース:
...
it "should add issue case 1" do
trip = FactoryGirl.build(:trip, :case_1)
p trip.object_id # 7...8660
subject.do_qa(trip)
expect(trip.issue_1).not_to be_nil
end
it "should add issue case 2" do
trip = FactoryGirl.build(:trip, :case_2)
p trip.object_id # 7...2780
subject.do_qa(trip)
expect(trip.issue_2).not_to be_nil
end
...
サンプルモジュール:
module Qa
...
def self.do_qa(trips)
p trips.object_id # Same is the object id in the calling test case
@trips ||= Array.wrap(trips)
@trips.each do |t|
p t.object_id # Always the object id from the first test case!
... # Checks for case 1 and case 2
end
end
...
end
モジュールは単に再評価されているため、ループがオブジェクトを再使用して、第二のテストケースを通過したことがないので最初のtrip
オブジェクトです。ループ内に新しいオブジェクトをインスタンス化するよう強制する方法はありますか?
ひどいやり方がありますが、より良い方法があるかもしれませんが、もっとコードを見る必要があります。 'Qa'の他の方法は' @trips'を参照していますか?どのクラスに 'Qa'が含まれていますか?あなたのテストでは 'subject 'とは何ですか? ([RSpecの主題は主に悪いことです。](http://stackoverflow.com/questions/38437162/whats-the-difference-between-rspecs-subject-and-let-when-should-theybebeused) ) –