Method: ChefSpec::API::Stubs#stub_command

Defined in:
lib/chefspec/api/stubs.rb

#stub_command(command, &block) ⇒ ChefSpec::CommandStub

Stub a shell command to return a particular value without shelling out to the system.

Examples:

stubbing a command to return true

stub_command('test -f /tmp/bacon').and_return(true)

stubbing a block that is evaluated at runtime

stub_command('test -f /tmp/bacon') { MyClass.check? }

stubbing a command that matches a pattern

stub_command(/test \-f/).and_return(true)

stubbing a command that raises an exception

stub_command('test -f /tmp/bacon').and_raise(SomeException)

Parameters:

  • command (String, Regexp)

    the command to stub

Returns:

  • (ChefSpec::CommandStub)


26
27
28
# File 'lib/chefspec/api/stubs.rb', line 26

def stub_command(command, &block)
  ChefSpec::Stubs::CommandRegistry.register(ChefSpec::Stubs::CommandStub.new(command, &block))
end