I have got this library get_full_name to return a full name.
# ../libraries/get_full_name.rb
module MyOrg
module GetFullName
def get_full_name(username)
full_name = "#{node['domain_prefix']}\\#{username}"
end
end
end
Chef::Recipe.send(:include, MyOrg::GetFullName)
And a ChefSpec test for the above library.
# ../spec/get_full_name_spec.rb
require 'spec_helper'
Dir['libraries/*.rb'].each { |f| require File.expand_path(f) }
RSpec.configure do |c|
c.include MyOrg::GetFullName
end
describe MyOrg::GetFullName do
let(:subject) { Class.new { extend MyOrg::GetFullName } }
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'windows', version: '2016') do |node|
node.default['domain_prefix'] = "Example"
end.converge(described_recipe)
end
it 'has valid full name' do
expect(get_full_name("jenkins")).to be("Example\\jenkins")
end
end
I have got this error when I ran chefspec
undefined local variable or method `node'
I dont know why it threw that error since I have already specified the node['domain_prefix'] in the ChefSpec.
Could anyone please help me point out what have I missed and is it the correct way to test and assert this type of return value? Would I be able to do the same if I want to test the library in the ChefSpec test for recipes which call this library's function?
Thanks