6

I have a simple test for the nginx cookbook:

require 'spec_helper'

describe 'my_cookbook::nginx' do
  let(:chef_run) do
    ChefSpec::Runner.new do |node|
      node.set['nginx']['dir'] = '/etc/nginx'
    end.converge(described_recipe)
  end

  it 'should create configuration directory' do
    expect(chef_run).to create_directory("#{node['nginx']['dir']}")
  end

end

Which is failing:

Failures:

  1) my_cookbook::nginx should create configuration directory
     Failure/Error: expect(chef_run).to create_directory("#{node['nginx']['dir']}")
     NameError:
       undefined local variable or method `node' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000007993570>

I'm attempting to set the node attributes as described in the docs, is there something obvious I'm missing?

ben lemasurier
  • 2,582
  • 4
  • 22
  • 37

1 Answers1

10

You are able to set node attributes. If you look at the stacktrace, it's complaining about this line:

expect(chef_run).to create_directory("#{node['nginx']['dir']}")

Specifically, #{node['nginx']['dir']}. You should use a static value here, otherwise your test is pointless. Change it to:

expect(chef_run).to create_directory('/etc/nginx')
sethvargo
  • 26,739
  • 10
  • 86
  • 156
  • Direct from the author! Thanks, Seth! – ben lemasurier Feb 13 '14 at 19:56
  • Seth, why then this is working for me **expect(chef_run).to create_directory("#{Chef::Config[:file_cache_path]}/jdk/")** But I have problem with my default attribute. I expect that resources, that are wrapped around IF, be executed, but they aren't. My node attribute is set to TRUE, but when running RSpec, it fails. Real test (kitchen test) is fine. – Lukino Apr 21 '15 at 22:29