2014-10-08 49 views
0

我知道我可以嘲笑的对象如下:如何模拟Rails中的关联对象?

Account.any_instance.expects(:type).returns("premium") 

我记得听到这被认为是一个贫穷的做法。我喜欢做类似

user = users(:bob) 
user.account.expects(:type).returns("premium") 

但这似乎并没有模拟正确的对象。有没有比在班上使用any_instance电话更好的解决方案?

编辑:那我收到错误消息是

not all expectations were satisfied 
unsatisfied expectations: 
- expected exactly once, not yet invoked: #<Account:0x5f9f8f8>.type(any_parameters) 
+0

我认为如果你展示整个方法和整个测试,情况会更好。现在它只能是一个猜测的问题。 – sebkkom 2014-10-08 14:56:44

回答

0

我仍然在寻找我的嘲笑的方式,但你尝试过这样的:

account = user.account 
account.expects(:type).returns("premium") 

这将有助于有你的测试背景知道你想嘲笑哪个对象。

+0

刚刚尝试过,谢谢。我收到了同样的错误消息,我会稍微添加到我的问题中。 – sakovias 2014-10-08 14:43:32