I'm including a custom gem in a chef cookbook. No matter what I've tried I'm get similar errors to undefined local variable or method `host'
I've tried many different variations to this.
allow(HostOpsCookbook).to receive(:host).with(:sonic.version).and_return('10.0')
Gem layout
cust_gem
\lib
\ibus
\host
sonic.rb
host.rb
host.rb
module Ibus
class Host
attr_reader :sonic
def initialize
extend_type
end
def enxtend_type
@sonic = Ibus::Host::Sonic.new
end
end
end
host\sonic.rb
module Ibus
class Host
class Sonic
def version
.....
end
end
end
end
Chef cookbook
cookbook
\libraries
host_helper.rb
\recipes
default.rb
chef\cookbook\libraries\host_helper.rb
module HostOpsCookbook
def host
require_ibus_gem #Loads the gem
@@host ||= Ibus::Host.new
end
end
Chef::Recipe.send(:include, HostOpsCookbook) if defined?(Chef::Recipe)
Chef::Resource.send(:include, HostOpsCookbook) if defined?(Chef::Resource)
Chef::Provider.send(:include, HostOpsCookbook) if defined?(Chef::Provider)
chef\cookbook\recipes\default.rb
sonic_version = host.sonic.version
This as code works the call to the gem method works.
However I can't figure out how to stub the below in the spec tests.
host.sonic.version
You need to install the required rubygem into the environment ChefSpec is running in.
This will make sure that the rubygem is resolved by ChefSpec.