我应该如何在全局使用RSpec存根方法? [英] How should I stub a method globally using RSpec?
问题描述
我正在开发Rails应用程序.我正在尝试在全球范围内存根方法.
I am working on a Rails application. I am trying to stub a method globally.
我正在做的是将其存入RSpec配置中的before(:suite)
块中,如下所示:
What I am doing is to stub it inside the RSpec configuration, on a before(:suite)
block as follows:
RSpec.configure do |config|
config.before(:suite) do
allow_any_instance_of(MyModel).to receive(:my_method).and_return(false)
end
end
但是,启动测试失败并显示以下错误:
However, starting the test fails with the following error:
in `method_missing': undefined method `allow_any_instance_of' for #<RSpec::Core::ExampleGroup:0x00000008d6be08> (NoMethodError)
in `method_missing': undefined method `allow_any_instance_of' for #<RSpec::Core::ExampleGroup:0x00000008d6be08> (NoMethodError)
有任何线索吗?我应该如何在全局使用RSpec存根方法?
Any clue? How should I stub a method globally using RSpec?
P.
推荐答案
这可能是上下文/初始化问题.在config.before(:each)
中执行此操作可以解决您的问题.
It probably is a context / initialization issue. Doing it in config.before(:each)
should solve your problem.
这篇关于我应该如何在全局使用RSpec存根方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!