如何在项目中模拟自编写模块的模块功能?
鉴于模块和功能
module ModuleA::ModuleB
def self.my_function( arg )
end
end
这就是所谓的
ModuleA::ModuleB::my_function( with_args )
当我在编写规范的函数中使用它时,我应该如何模拟它?
加倍(obj = double("ModuleA::ModuleB")
)对我来说没有意义,因为函数是在模块上调用而不是在对象上调用。
我试过掐它(ModuleA::ModuleB.stub(:my_function).with(arg).and_return(something)
)。显然,它没有用。 stub
那里没有定义。
然后我试了一下 should_receive
。再次 NoMethodError
。
模拟模块及其功能的首选方法是什么?
给出您在问题中描述的模块
module ModuleA ; end
module ModuleA::ModuleB
def self.my_function( arg )
end
end
和被测函数,它调用模块函数
def foo(arg)
ModuleA::ModuleB.my_function(arg)
end
然后你可以测试一下 foo
电话 myfunction
喜欢这个:
describe :foo do
it "should delegate to myfunction" do
arg = mock 'arg'
result = mock 'result'
ModuleA::ModuleB.should_receive(:my_function).with(arg).and_return(result)
foo(arg).should == result
end
end
给出您在问题中描述的模块
module ModuleA ; end
module ModuleA::ModuleB
def self.my_function( arg )
end
end
和被测函数,它调用模块函数
def foo(arg)
ModuleA::ModuleB.my_function(arg)
end
然后你可以测试一下 foo
电话 myfunction
喜欢这个:
describe :foo do
it "should delegate to myfunction" do
arg = mock 'arg'
result = mock 'result'
ModuleA::ModuleB.should_receive(:my_function).with(arg).and_return(result)
foo(arg).should == result
end
end
对于rspec 3.6,请参阅 如何在RSpec中模拟类方法期望语法?
为了避免仅链接答案,这里是Andrey Deineko的答案副本:
allow(Module)
.to receive(:profile)
.with("token")
.and_return({"name" => "Hello", "id" => "14314141", "email" => "hello@me.com"})