如何在模块中存根方法:
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_two的返回值来单独测试method_one
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
endSomeController中包含的SomeModule规范会失败,因为method_two会抛出异常(它会尝试执行尚未设定种子的数据库查找)。
在method_one中调用method_two时,如何对其进行存根
发布于 2013-09-21 02:22:03
shared_examples_for SomeModule do
let(:instance) { described_class.new }
it 'does something exciting' do
instance.should_receive(:method_two).and_return('MANUAL')
expect(instance.method_one).to eq(some_value)
end
end
describe SomeController do
include_examples SomeModule
end发布于 2015-06-11 20:31:52
allow_any_instance_of(M).to receive(:foo).and_return(:bar)Is there a way to stub a method of an included module with Rspec?
这种方法对我很有效。
https://stackoverflow.com/questions/18333821
复制相似问题