Module: ChefSpec::API::StubsFor

Extended by:
ClassMethods, RSpec::SharedContext
Defined in:
lib/chefspec/api/stubs_for.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

HAS_SHELLOUT_COMPACTED =

Which version to use the shell_out_compacted hook on.

Gem::Requirement.create("> 14.2")

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

included

Class Method Details

.setup_stubs_for(object, type) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Hook used in the monkey patches to set up a place to inject stubs when needed for a resource or provider.

Parameters:

  • object (Chef::Resource, Chef::Provider)

    Resource or provider to inject

  • type (Symbol)

    Type of object to register stubs on



20
21
22
# File 'lib/chefspec/api/stubs_for.rb', line 20

def self.setup_stubs_for(object, type)
  # This space left intentionally blank, real implementation is below.
end

Instance Method Details

#receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chefspec/api/stubs_for.rb', line 89

def receive_shell_out(*cmd, stdout: "", stderr: "", exitstatus: 0, **opts)
  # Ruby does not allow constructing an actual exitstatus object from Ruby code. Really.
  fake_exitstatus = double(exitstatus: exitstatus)
  fake_cmd = Mixlib::ShellOut.new(*cmd)
  fake_cmd.define_singleton_method(:run_command) {} # Do nothing, just in case.
  # Inject our canned data.
  fake_cmd.instance_exec do
    @stdout = stdout
    @stderr = stderr
    @status = fake_exitstatus
  end
  # On newer Chef, we can intercept using the new, better shell_out_compact hook point.
  shell_out_method ||= if HAS_SHELLOUT_COMPACTED.satisfied_by?(Gem::Version.create(Chef::VERSION))
                         :shell_out_compacted
                       else
                         :shell_out
                       end
  with_args = cmd + (opts.empty? ? [any_args] : [hash_including(opts)])
  receive(shell_out_method).with(*with_args).and_return(fake_cmd)
end

#stubs_for_current_value(target = nil, &block) ⇒ void Also known as: stubs_for_current_resource

This method returns an undefined value.

Register stubs for current_value objects.

Parameters:

  • target (String, nil) (defaults to: nil)

    Resource name to inject, or nil for all resources.

  • block (Proc)

    A block taking the resource object as a parameter.

See Also:



74
75
76
# File 'lib/chefspec/api/stubs_for.rb', line 74

def stubs_for_current_value(target = nil, &block)
  _chefspec_stubs_for_registry[:current_value][target] << block
end

#stubs_for_provider(target = nil, &block) ⇒ void

This method returns an undefined value.

Register stubs for provider objects.

Parameters:

  • target (String, nil) (defaults to: nil)

    Resource name to inject, or nil for all providers.

  • block (Proc)

    A block taking the resource object as a parameter.

See Also:



85
86
87
# File 'lib/chefspec/api/stubs_for.rb', line 85

def stubs_for_provider(target = nil, &block)
  _chefspec_stubs_for_registry[:provider][target] << block
end

#stubs_for_resource(target = nil, current_value: true, current_resource: true, &block) ⇒ void

This method returns an undefined value.

Register stubs for resource objects.

The ‘target` parameter can select either a resource string like `’package’‘, a resource name like `’package’‘, or `nil` for all resources.

Examples:

Setting method stub on a single resource

it "does a thing" do
  stubs_for_resource("my_resource[foo]") do |res|
    expect(res).to receive_shell_out.with("my_command").and_return(stdout: "asdf")
  end
  expect(subject.some_method).to eq "asdf"
end

Parameters:

  • target (String, nil) (defaults to: nil)

    Resource name to inject, or nil for all resources.

  • current_value (Boolean) (defaults to: true)

    If true, also register stubs for current_value objects on the same target.

  • block (Proc)

    A block taking the resource object as a parameter.



62
63
64
65
66
# File 'lib/chefspec/api/stubs_for.rb', line 62

def stubs_for_resource(target = nil, current_value: true, current_resource: true, &block)
  current_value = false unless current_resource
  _chefspec_stubs_for_registry[:resource][target] << block
  stubs_for_current_value(target, &block) if current_value
end