Module: ChefSpec::Macros
Instance Method Summary collapse
-
#described_cookbook ⇒ String
The name of the currently running cookbook spec.
-
#described_recipe ⇒ String
The name of the currently running recipe spec.
-
#stub_command(command, &block) ⇒ ChefSpec::CommandStub
Stub a shell command to return a particular value without shelling out to the system.
-
#stub_data_bag(bag, &block) ⇒ ChefSpec::DataBagStub
Stub a Chef call to load a data_bag.
-
#stub_data_bag_item(bag, id, &block) ⇒ ChefSpec::DataBagItemStub
Stub a Chef data_bag call.
-
#stub_node(name = 'node.example', options = {}, &block) ⇒ Chef::Node
Creates a fake Chef::Node for use in testing.
-
#stub_search(type, query = '*:*', &block) ⇒ ChefSpec::SearchStub
Stub a Chef search to return pre-defined data.
Instance Method Details
#described_cookbook ⇒ String
The name of the currently running cookbook spec. Given the top-level describe
block is of the format:
describe 'my_cookbook::my_recipe' do
# ...
end
The value of described_cookbook
is “my_cookbook”.
23 24 25 |
# File 'lib/chefspec/macros.rb', line 23 def described_cookbook described_recipe.split('::').first end |
#described_recipe ⇒ String
The name of the currently running recipe spec. Given the top-level describe
block is of the format:
describe 'my_cookbook::my_recipe' do
# ...
end
The value of described_recipe
is “my_cookbook::my_recipe”.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/chefspec/macros.rb', line 43 def described_recipe scope = self.is_a?(Class) ? self : self.class = scope. while .has_key?(:example_group) = [:example_group] end [:description_args].first.to_s end |
#stub_command(command, &block) ⇒ ChefSpec::CommandStub
Stub a shell command to return a particular value without shelling out to the system.
76 77 78 |
# File 'lib/chefspec/macros.rb', line 76 def stub_command(command, &block) Stubs::CommandRegistry.register(Stubs::CommandStub.new(command, &block)) end |
#stub_data_bag(bag, &block) ⇒ ChefSpec::DataBagStub
Stub a Chef call to load a data_bag.
104 105 106 |
# File 'lib/chefspec/macros.rb', line 104 def stub_data_bag(bag, &block) Stubs::DataBagRegistry.register(Stubs::DataBagStub.new(bag, &block)) end |
#stub_data_bag_item(bag, id, &block) ⇒ ChefSpec::DataBagItemStub
Stub a Chef data_bag call.
131 132 133 |
# File 'lib/chefspec/macros.rb', line 131 def stub_data_bag_item(bag, id, &block) Stubs::DataBagItemRegistry.register(Stubs::DataBagItemStub.new(bag, id, &block)) end |
#stub_node(name = 'node.example', options = {}, &block) ⇒ Chef::Node
Creates a fake Chef::Node for use in testing.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/chefspec/macros.rb', line 172 def stub_node(name = 'node.example', = {}, &block) fauxhai = Fauxhai.mock().data fauxhai = fauxhai.merge([:ohai] || {}) fauxhai = Mash.new(fauxhai) node = Chef::Node.new node.name(name) node.automatic_attrs = fauxhai node.instance_eval(&block) if block_given? node end |
#stub_search(type, query = '*:*', &block) ⇒ ChefSpec::SearchStub
Stub a Chef search to return pre-defined data. When providing a value, the value is automatically mashified (to the best of ChefSpec’s abilities) to ease in use.
215 216 217 |
# File 'lib/chefspec/macros.rb', line 215 def stub_search(type, query = '*:*', &block) Stubs::SearchRegistry.register(Stubs::SearchStub.new(type, query, &block)) end |