Unit testing 如何在chefspec中模拟用另一个类扩展的对象实例的函数调用
我不熟悉单元测试和chefspec。我试图模拟/截获来自从属库的配方中的函数调用Unit testing 如何在chefspec中模拟用另一个类扩展的对象实例的函数调用,unit-testing,chef-infra,chefspec,Unit Testing,Chef Infra,Chefspec,我不熟悉单元测试和chefspec。我试图模拟/截获来自从属库的配方中的函数调用 图书馆 module Helper def do_something_useful return "http://example.com/file.txt" end end 配方 remote_file '/save/file/here' do extend Helper source do_something_useful end 我尝试了以下方法: Chefspec all
- 图书馆
module Helper def do_something_useful return "http://example.com/file.txt" end end
- 配方
remote_file '/save/file/here' do extend Helper source do_something_useful end
- Chefspec
我也试过用替身来嘲弄:allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:do_something_useful).and_return('foobar') allow_any_instance_of(Chef::Resource).to receive(:do_something_useful).and_return('foobar')
如果helper = double Helper.stub(:new).and_return(helper) allow(helper).to receive(:do_something_useful).and_return('foobar')
未初始化的常量帮助器
extend
覆盖了mock方法。因此,我们可以使用extend
来驱动:
before do
allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:extend) do |this, m|
Module.instance_method(:extend).bind(this).call(m)
allow(this).to receive(:do_something_useful).and_return('foobar')
end
end
这就像是800%的魔法,您可能不应该使用它,但它在我的小测试环境中确实有效