在写Rspec测试时,我经常对should_receive
个测试感到沮丧.我想知道是否有一个不那么麻烦的替代方案.
例如:
describe "making a cake" do
it "should use some other methods" do
@baker.should_receive(:make_batter)
@baker.make_cake
end
end
对should_receive
的调用是一个很好的描述,但它 destruct 了我的代码,因为should_receive
通过屏蔽原始方法工作,而make_cake
不能继续,除非make_batter
实际返回一些batter.所以我把它改成:
@baker.should_receive(:make_batter).and_return(@batter)
这很难看,因为:
- 这就像我在测试
make_batter
正确返回@batter
,但我是actually强迫make_batter
的假版本返回它. - 它迫使我单独设置
@batter
个 - 如果
make_batter
有任何重要的副作用(我想可能是代码气味),我也必须让它们发生.
我希望should_receive(:make_batter)
验证方法调用,pass it on to the original method验证方法调用.如果我想存根它的行为以进行更好的隔离测试,我会明确地这样做:@baker.stub(:make_batter).and_return(@batter)
.
有没有一种方法可以在不阻止原始方法调用的情况下执行类似于should_receive
的操作?我的问题是设计不好的症状吗?