class Mocha::Configuration

This class allows you to determine what should happen under certain circumstances. In each scenario, Mocha can be configured to {.allow do nothing}, {.warn_when display a warning message}, or {.prevent raise an exception}. The relevant scenario is identified using one of the following symbols:

@example Preventing unnecessary stubbing of a method

Mocha::Configuration.prevent(:stubbing_method_unnecessarily)

example = mock('example')
example.stubs(:unused_stub)
# => Mocha::StubbingError: stubbing method unnecessarily:
# =>   #<Mock:example>.unused_stub(any_parameters)

@example Preventing stubbing of a method on a non-mock object

Mocha::Configuration.prevent(:stubbing_method_on_non_mock_object)

class Example
  def example_method; end
end

example = Example.new
example.stubs(:example_method)
# => Mocha::StubbingError: stubbing method on non-mock object:
# =>   #<Example:0x593620>.example_method

@example Preventing stubbing of a non-existent method

Mocha::Configuration.prevent(:stubbing_non_existent_method)

class Example
end

example = Example.new
example.stubs(:method_that_doesnt_exist)
# => Mocha::StubbingError: stubbing non-existent method:
# =>   #<Example:0x593760>.method_that_doesnt_exist

@example Preventing stubbing of a non-public method

Mocha::Configuration.prevent(:stubbing_non_public_method)

class Example
  def internal_method; end
  private :internal_method
end

example = Example.new
example.stubs(:internal_method)
# => Mocha::StubbingError: stubbing non-public method:
# =>   #<Example:0x593530>.internal_method

Typically the configuration would be set globally in a test_helper.rb or spec_helper.rb file. However, it can also be temporarily overridden locally using the block syntax of the relevant method. In the latter case, the original configuration settings are restored when the block is exited.

@example Temporarily allowing stubbing of a non-existent method

Mocha::Configuration.prevent(:stubbing_non_public_method)

class Example
end

Mocha::Configuration.allow(:stubbing_non_existent_method) do
  example = Example.new
  example.stubs(:method_that_doesnt_exist)
  # => no exception raised
end

Constants

DEFAULTS

Public Class Methods

allow(action, &block) click to toggle source

Allow the specified action.

@param [Symbol] action one of :stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method, :stubbing_method_on_nil. @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.

# File lib/mocha/configuration.rb, line 82
def allow(action, &block)
  change_config action, :allow, &block
end
allow?(action) click to toggle source

@private

# File lib/mocha/configuration.rb, line 87
def allow?(action)
  configuration[action] == :allow
end
prevent(action, &block) click to toggle source

Raise a {StubbingError} if if the specified action is attempted.

@param [Symbol] action one of :stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method, :stubbing_method_on_nil. @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.

# File lib/mocha/configuration.rb, line 108
def prevent(action, &block)
  change_config action, :prevent, &block
end
prevent?(action) click to toggle source

@private

# File lib/mocha/configuration.rb, line 113
def prevent?(action)
  configuration[action] == :prevent
end
reset_configuration() click to toggle source

@private

# File lib/mocha/configuration.rb, line 118
def reset_configuration
  @configuration = nil
end
warn_when(action, &block) click to toggle source

Warn if the specified action is attempted.

@param [Symbol] action one of :stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method, :stubbing_method_on_nil. @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.

# File lib/mocha/configuration.rb, line 95
def warn_when(action, &block)
  change_config action, :warn, &block
end
warn_when?(action) click to toggle source

@private

# File lib/mocha/configuration.rb, line 100
def warn_when?(action)
  configuration[action] == :warn
end

Private Class Methods

change_config(action, new_value, &block) click to toggle source

@private

# File lib/mocha/configuration.rb, line 130
def change_config(action, new_value, &block)
  if block_given?
    temporarily_change_config action, new_value, &block
  else
    configuration[action] = new_value
  end
end
configuration() click to toggle source

@private

# File lib/mocha/configuration.rb, line 125
def configuration
  @configuration ||= DEFAULTS.dup
end
temporarily_change_config(action, new_value) { || ... } click to toggle source

@private

# File lib/mocha/configuration.rb, line 139
def temporarily_change_config(action, new_value)
  original_value = configuration[action]
  configuration[action] = new_value
  yield
ensure
  configuration[action] = original_value
end