module Poise::Helpers::ChefspecMatchers

A resource mixin to register ChefSpec matchers for a resource automatically.

If you are using the provides() form for naming resources, ensure that is set before declaring actions.

@since 2.0.0 @example Define a class

class Chef::Resource::MyResource < Chef::Resource
  include Poise::Helpers::ChefspecMatchers
  actions(:run)
end

@example Use a matcher

expect(chef_run).to run_my_resource('...')

Public Class Methods

create_matcher(resource, action) click to toggle source

Create a matcher for a given resource type and action. This is idempotent so if a matcher already exists, it will not be recreated.

@api private

# File lib/poise/helpers/chefspec_matchers.rb, line 48
def self.create_matcher(resource, action)
  # Check that we have everything we need.
  return unless defined?(ChefSpec) && defined?(RSpec::Matchers) && resource
  method = :"#{action}_#{resource}"
  return if RSpec::Matchers.method_defined?(method)
  RSpec::Matchers.send(:define_method, method) do |resource_name|
    ChefSpec::Matchers::ResourceMatcher.new(resource, action, resource_name)
  end
end