0

我知道我可以按如下方式模拟对象:

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

我记得听说这被认为是一种糟糕的做法。我想做类似的事情

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

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

编辑:我得到的错误信息是

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

1 回答 1

0

我仍在寻找模拟方式,但您是否尝试过类似的方法:

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

让您的测试上下文知道您想要模拟哪个对象会有所帮助。

于 2014-10-08T14:15:05.137 回答