4

すべてのインスタンスではなく、特定のオブジェクトだけではなく、特定のモデルだけをスタブ化したい

たとえば、属性 'name' (文字列) と 'cool' (ブール値) を持つクラス 'Person' を指定します。2 つのモデルがあります。

person_bill:
   name: bill
   cool: false

person_steve:
   name: steve
   cool: false

ここで、問題なく動作する steve だけをスタブしたいと思います。

p1 = people(:person_steve)
p1.stubs(:cool? => true)
assert p1.cool? #works

しかし、DB からモデルを再度ロードすると、次のようにはなりません。

p1 = people(:person_steve)
p1.stubs(:cool? => true)
p1 = Person.find_by_name p1.name
assert p1.cool? #fails!!

これは機能しますが、 Billにも影響します。

 Person.any_instance.stubs(:cool? => true)
 assert people(:person_bill).cool? #doesn't fails although it should

では、どうすればスティーブをスタブにすることができますか? のような条件付きの any_instance はありますか

 Person.any_instance { |p| p.name == 'Steve' }.stubs(:cool? => true)

前もって感謝します!

4

1 に答える 1