给定一个父类Fruit
及其子类Apple
and Banana
,是否可以存根foo
定义在 中的方法Fruit
,以便对and的foo
任何实例的任何方法调用都被存根?Apple
Banana
class Fruit
def foo
puts "some magic in Fruit"
end
end
class Banana < Fruit
...
end
class Apple < Fruit
...
end
Fruit.any_instance.stubs(:foo)
没有工作,看起来它只是水果实例的存根。除了为每个子类调用存根之外,有没有一种简单的方法来实现这一点?
发现这个链接提出了类似的问题,但看起来还没有回答。 http://groups.google.com/group/mocha-developer/browse_thread/thread/99981af7c86dad5e