gpt4 book ai didi

ruby-on-rails - Rspec - stub 模块方法

转载 作者:行者123 更新时间:2023-12-03 20:55:04 26 4
gpt4 key购买 nike

如何 stub 模块中的方法:

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也可以通过 stub 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引发异常(它尝试进行尚未播种的数据库查找)。

我如何 stub method_twomethod_one 内调用时?

最佳答案

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?

这种方法对我有用

关于ruby-on-rails - Rspec - stub 模块方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18333821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com