如何存根模块中的方法:
module SomeModule
def method_one
# do stuff
something = method_two(some_arg)
# so more stuff
end
def method_two(arg)
# do stuff
end
end
我可以单独测试method_two
。
我也想method_one
通过存根的返回值来单独测试method_two
:
shared_examples_for SomeModule do
it 'does something exciting' do
# neither of the below work
# SomeModule.should_receive(:method_two).and_return('MANUAL')
# SomeModule.stub(:method_two).and_return('MANUAL')
# expect(described_class.new.method_one).to eq(some_value)
end
end
describe SomeController do
include_examples SomeModule
end
其中的规范SomeModule
包含在SomeController
失败中,因为method_two
引发异常(它尝试进行尚未播种的数据库查找)。
method_two
在内部调用它时如何存根method_one
?