过去几周我一直在编写ChefSpec单元测试套件并且已经设法做了很多,但是我发现了一个令我难过的场景。我的食谱包含'aws'食谱的默认食谱,然后安装right_aws
,然后进入require
。
但ChefSpec运行器似乎遇到了问题,吐出了Ruby LoadError:
LoadError
---------
cannot load such file -- right_aws
Cookbook Trace:
---------------
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/aws/recipes/default.rb:25:in `from_file'
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/acmecorp-postgresql/recipes/server.rb:71:in `from_file'
Relevant File Content:
----------------------
/var/folders/0r/cg1hmpkj2nb3wh6slrg1hkhm0000gn/T/d20140612-36208-q1ecjj/cookbooks/aws/recipes/default.rb:
18: #
19:
20: chef_gem "right_aws" do
21: version node['aws']['right_aws_version']
22: action :install
23: end
24:
25>> require 'right_aws'
26:
有没有办法在我的测试中模拟'aws'食谱中的require 'right_aws'
行?推荐吗?
在运行ChefSpec测试的系统上简单地安装right_aws gem是否更好?
答案 0 :(得分:3)
您有几个选择:
right_aws
来确保allow_any_instance_of(Chef::Recipe).to receive(:include_recipe)
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with('aws').and_return(true)
可用(如问题评论中所述)您可以直接模拟对include_recipe的调用(如果您不需要包含测试的配方)
allow(Kernel).to receive(:require)
allow(Kernel).to receive(:require).with("right_aws").and_return(true)
模拟需求调用本身。
{{1}}
如果可能的话,我建议#2