I've been writing ChefSpec unit test suites for the past couple of weeks and have managed to do quite a bit with it, but I have found a scenario that got me stumped. I have a recipe that includes the 'aws' cookbook's default recipe, which in turn installs the right_aws
and then proceeds to require
it.
But the ChefSpec runner seems to have problems with it, spitting out a 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:
Is there a way to mock that require 'right_aws'
line from the 'aws' cookbook in my test? Is it recommended?
Is it better to simply install the right_aws gem on the system running ChefSpec tests?
You have a few options:
right_aws
is available by installing it directly on your machine or with a Gemfile (as noted in the question comments)You can mock the call to include_recipe directly (if you don't need the included recipe for your test)
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)
Mock the requires call itself.
allow(Kernel).to receive(:require)
allow(Kernel).to receive(:require).with("right_aws").and_return(true)
If possible, I'd recommend #2