Briankurt
Briankurt

Reputation: 1

Chefspec defining local variable or attributes

i am writing chefspec tests and i have below code

   recipe


execute 'stop Server' do
  user '123'
  group 'ad'
  live_stream true
  command "/udd001/app/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh $server -username wasserver -password #{password}"
  only_if { ::File.exist?('/mypath/'+variable[:myserver) }


     spec

require 'spec_helper'

describe 'cookbook::myserver' do
  before do
    allow(File).to receive(:exist?).and_call_original
    allow(File).to receive(:exist?).with('/mypath/'+variable[:appserver_profile]).and_return(true)
  end

  context 'When all attributes are default, on Ubuntu 16.04' do
    let(:chef_run) do
      runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04')
      runner.converge(described_recipe)
    end

    it 'converges successfully' do
      expect { chef_run }.to_not raise_error
    end

    it 'stop Server' do
      expect(chef_run).to run_execute('myserver').with(
        user: '123',
        group: 'ad',
        live_stream: true)
    end
  end

end

it fails with local variable "variable" is not defined

Upvotes: 0

Views: 469

Answers (1)

coderanger
coderanger

Reputation: 54249

You cannot use local variables from your recipe (I'm assuming that variable was defined further up in your recipe code, but you didn't actually show that) in your specs like that. In fact you shouldn't, since the whole point of a test is to compare to known outputs, so what you want to do is go through and work out what the full path should be, and then put that in the test. Test code is somewhat exempt from the "don't repeat yourself" rule, as the whole point is trust via repetition.

Upvotes: 0

Related Questions