我只想存根一个特定的模型,但不仅仅是一个特定的对象,而不是每个实例
例如,给定类“Person”,其属性为“name”(字符串)和“cool”(布尔值)。我们有两个模型:
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
但是,如果我再次从数据库加载模型,它不会:
p1 = people(:person_steve)
p1.stubs(:cool? => true)
p1 = Person.find_by_name p1.name
assert p1.cool? #fails!!
这有效,但也会影响比尔,这不应该:
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)
提前致谢!