存储include_recipe调用以不执行任何操作,但仍将计数包括在内

时间:2013-06-19 17:25:05

标签: ruby rspec chef chefspec

我的食谱的默认配方只包括其他几个食谱。我知道我可以使用以下方法测试合适的配方:

expect(chef_run).to include_recipe 'cookbook::recipe_name

但是当我像这样删除include_recipe调用

时,这不起作用

Chef::Recipe.any_instance.stub(:include_recipe).with(anything).and_return(true)

我正在对include_recipe调用进行存根,因为我不想在此规范中为我的所有食谱定义所有缺少的属性。

我原本以为通过存根方法来返回true ChefSpec会报告它已经运行并考虑包含的配方,但事实并非如此。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我打算将上面的帖子作为一个问题发布,但我在github上发现了一个由{RoboticCheese'发布的solution,所以我回答了我自己的问题,因为它可能对其他人有所帮助。

您可以使用上面的存根包含配方,但您还需要存根一些RunContext方法来计算已包含的配方。

答案 1 :(得分:0)

如果您想确保使用正确的食谱调用include_recipe,但我不想在包含的食谱中加载资源,我认为我有更新且更清洁的解决方案(词):

before(:all) { @included_recipes = [] }
before do
  @stubbed_recipes = %w[test_cookbook::included_recipe apt]
  @stubbed_recipes.each do |r|
    allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with(r) do
      @included_recipes << r
    end
  end
end

it 'includes the correct recipes' do
  chef_run
  expect(@included_recipes).to match_array(@stubbed_recipes)
end

完整的例子:https://github.com/atheiman/test-cookbook/pull/4