摩卡咖啡和嵌套对象 [英] mocha and nested objects
本文介绍了摩卡咖啡和嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不好意思,如果这是一个愚蠢的问题,我是新来的嘲弄者.
Excuse if this is a silly question, I am new to mocking.
我可以使用摩卡(mocha)做类似的事情:
I am able to use mocha to do things like:
person.expects(:first_name).returns('David')
如何模拟嵌套对象?
说我有一个属于某个人的产品,我想获得该人的名字.
Say I have a Product that belongs to a Person and I want to get the first name of that person.
在我的应用中,我可能会这样:
In my app I might do it like this:
product.person.first_name
我如何通过模拟获得相同的结果?
How would I get the same result using a mock?
推荐答案
作为shingara答案的替代方法,您可以使用mocha的any_instance
方法它将检测对任何类实例的调用".
as an alternative to shingara's answer, you could use mocha's any_instance
method "which will detect calls to any instance of a class".
Person.any_instance.expects(:first_name).returns('david')
记录在:
http://mocha.rubyforge.org/classes/Mocha/ClassMethods.html# M000001
这篇关于摩卡咖啡和嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文