unit-testingrspecchef-infrachefspec

Unable to run rspec unit test "touch_file" in chef cookbook


Cookbook: https://github.com/tkidd77/devops-project/tree/master/chef-repo/cookbooks/hello_world

My unit test:

it 'touches the correct file' do
  expect { chef_run }.to touch_file ('C:\inetpub\wwwroot\iisstart.htm')
end

Output when running "chef exec rspec" in git bash:

tkidd@tkiddhome MINGW64 /c/git/project/chef-repo/cookbooks/hello_world (master) $ chef exec rspec .F

Failures:

  1) hello_world::default touches the correct file
     Failure/Error: expect { chef_run }.to touch_file ('C:\inetpub\wwwroot\iisstart.htm')
       You must pass an argument rather than a block to use the provided matcher ( file "C:\inetpub\wwwroot\iisstart.htm"), or the matcher must implement `supports_block_expectations?`.
     # ./spec/unit/recipes/default_spec.rb:38:in `block (2 levels) in <top (required)>'

Finished in 0.07199 seconds (files took 8.68 seconds to load) 2 examples, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:37 # hello_world::default touches the correct file

Here is the chefspec documentation on using the touch_file test: https://www.rubydoc.info/github/acrmp/chefspec/ChefSpec/API/FileMatchers which specifies using parenthesis instead of brackets around "chef-run", but when I do that, I receive an "undefined method" error:

tkidd@tkiddhome MINGW64 /c/git/project/chef-repo/cookbooks/hello_world (master) $ chef exec rspec .F

Failures:

  1) hello_world::default touches the correct file
     Failure/Error: expect (chef_run).to touch_file ('C:\inetpub\wwwroot\iisstart.htm')

     NoMethodError:
       undefined method `to' for #<ChefSpec::SoloRunner:0x0000000007a241d8>
     # ./spec/unit/recipes/default_spec.rb:38:in `block (2 levels) in <top (required)>'

Finished in 0.04001 seconds (files took 4.92 seconds to load) 2 examples, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:37 # hello_world::default touches the correct file

According to this, rspec 3.0 expects a method instead of a block for the file path, but I don't understand what that would look like. How to check whether a variable is an instance of a module's subclass using rspec?


Solution

  • That should be expect(chef_run).to touch_file('C:\inetpub\wwwroot\iisstart.htm'). You only use expect { ... } with raise_error and things like it, most matches use expect(...) as a normal call. Also you had an extra space after touch_file. method (args) is not allowed in Ruby (or at least it is allowed and doesn't do what you think it does).