【发布时间】:2015-12-05 10:35:43
【问题描述】:
我有以下代码:
class Blurk
def initialize
# horror
end
def perform
# yet more horror
end
end
class Grunf < Blurk
def perform
super
# here some code to test
end
end
我想在Grunf#perform 上测试代码,但我不知道如何模拟Blurk。
【问题讨论】:
-
您可以在测试
Grunf时重新定义Blurk#perform,或者(我认为,更好),您可以将Blurk作为一个单元进行测试,并将Grunf也作为一个单元进行测试。也就是说,不假设它是否是其他东西的子类,而是假设“其余部分是有效的”。 -
在RSpec中,你也可以尝试
allow().to_receive().and_return()的组合来控制对super的调用。