class Mocha::Configuration


end
c.stubbing_method_on_nil = :allow
c.stubbing_method_on_non_mock_object = :warn
c.stubbing_method_unnecessarily = :prevent
Mocha.configure do |c|
@example Setting multiple configuration options
Typically the configuration is set globally in a test_helper.rb or spec_helper.rb file.
This class provides a number of ways to configure the library.

def change_config(action, new_value, &block)

Other tags:
    Private: -
def change_config(action, new_value, &block)
  if block_given?
    temporarily_change_config action, new_value, &block
  else
    configuration.send("#{action}=".to_sym, new_value)
  end
end

def configuration

Other tags:
    Private: -
def configuration
  @configuration ||= new
end

def display_matching_invocations_on_failure=(value)

Other tags:
    Example: Enable display of matching invocations -

Parameters:
  • value (Boolean) -- +true+ to enable display of matching invocations; disabled by default.
def display_matching_invocations_on_failure=(value)
  @options[:display_matching_invocations_on_failure] = value
end

def display_matching_invocations_on_failure?

Other tags:
    Private: -
def display_matching_invocations_on_failure?
  @options[:display_matching_invocations_on_failure]
end

def initialize(options = {})

Other tags:
    Private: -
def initialize(options = {})
  @options = DEFAULTS.merge(options)
end

def initialize_copy(other)

Other tags:
    Private: -
def initialize_copy(other)
  @options = other.options.dup
end

def merge(other)

Other tags:
    Private: -
def merge(other)
  self.class.new(@options.merge(other.options))
end

def override(temporary_options)

Other tags:
    Example: Temporarily allow stubbing of +nil+ -

Other tags:
    Yield: - block during which the configuration change will be in force.

Parameters:
  • temporary_options (Hash) -- the configuration options to apply for the duration of the block.
def override(temporary_options)
  original_configuration = configuration
  @configuration = configuration.merge(new(temporary_options))
  yield
ensure
  @configuration = original_configuration
end

def reset_configuration

Other tags:
    Private: -
def reset_configuration
  @configuration = nil
end

def strict_keyword_argument_matching=(value)

Other tags:
    Example: Strict keyword argument matching -
    Example: Loose keyword argument matching (default) -

Parameters:
  • value (Boolean) -- +true+ to enable strict keyword argument matching; +false+ by default.
def strict_keyword_argument_matching=(value)
  raise 'Strict keyword argument matching requires Ruby 2.7 and above.' unless Mocha::RUBY_V27_PLUS
  @options[:strict_keyword_argument_matching] = value
end

def strict_keyword_argument_matching?

Other tags:
    Private: -
def strict_keyword_argument_matching?
  @options[:strict_keyword_argument_matching]
end

def stubbing_method_on_nil

Other tags:
    Private: -
def stubbing_method_on_nil
  @options[:stubbing_method_on_nil]
end

def stubbing_method_on_nil=(value)

Parameters:
  • value (Symbol) -- one of +:allow+, +:warn+, +:prevent+.
def stubbing_method_on_nil=(value)
  @options[:stubbing_method_on_nil] = value
end

def stubbing_method_on_non_mock_object

Other tags:
    Private: -
def stubbing_method_on_non_mock_object
  @options[:stubbing_method_on_non_mock_object]
end

def stubbing_method_on_non_mock_object=(value)

Other tags:
    Example: Preventing stubbing of a method on a non-mock object -

Parameters:
  • value (Symbol) -- one of +:allow+, +:warn+, +:prevent+.
def stubbing_method_on_non_mock_object=(value)
  @options[:stubbing_method_on_non_mock_object] = value
end

def stubbing_method_unnecessarily

Other tags:
    Private: -
def stubbing_method_unnecessarily
  @options[:stubbing_method_unnecessarily]
end

def stubbing_method_unnecessarily=(value)

Other tags:
    Example: Preventing unnecessary stubbing of a method -

Parameters:
  • value (Symbol) -- one of +:allow+, +:warn+, +:prevent+.
def stubbing_method_unnecessarily=(value)
  @options[:stubbing_method_unnecessarily] = value
end

def stubbing_non_existent_method

Other tags:
    Private: -
def stubbing_non_existent_method
  @options[:stubbing_non_existent_method]
end

def stubbing_non_existent_method=(value)

Other tags:
    Example: Preventing stubbing of a non-existent method -

Parameters:
  • value (Symbol) -- one of +:allow+, +:warn+, +:prevent+.
def stubbing_non_existent_method=(value)
  @options[:stubbing_non_existent_method] = value
end

def stubbing_non_public_method

Other tags:
    Private: -
def stubbing_non_public_method
  @options[:stubbing_non_public_method]
end

def stubbing_non_public_method=(value)

Other tags:
    Example: Preventing stubbing of a non-public method -

Parameters:
  • value (Symbol) -- one of +:allow+, +:warn+, +:prevent+.
def stubbing_non_public_method=(value)
  @options[:stubbing_non_public_method] = value
end

def temporarily_change_config(action, new_value)

Other tags:
    Private: -
def temporarily_change_config(action, new_value)
  original_configuration = configuration
  new_configuration = configuration.dup
  new_configuration.send("#{action}=".to_sym, new_value)
  @configuration = new_configuration
  yield
ensure
  @configuration = original_configuration
end