代码之家  ›  专栏  ›  技术社区  ›  RngTng

Rails Test&Mocha:如何在任何\u实例中存根特定的模型条件?

  •  4
  • RngTng  · 技术社区  · 16 年前

    具体的 模型,但是 只有特定的对象和 每一个实例

    person_bill:
       name: bill
       cool: false
    
    person_steve:
       name: steve
       cool: false
    

    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!!
    

    这是可行的,但会影响 比尔 同样,这不应该:

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

    那我怎么能就这么说呢,但无论如何?有没有一个像这样的有条件的例子

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

    提前谢谢!

    1 回复  |  直到 5 年前
        1
  •  7
  •   mixonic    16 年前

    Person.stubs( :find_by_name ).
           returns( stub(:cool? => true) )
    

    你能再举一个例子说明你在找什么吗?祝你好运!

    推荐文章