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)
- 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
- Private: -
def configuration @configuration ||= new end
def display_matching_invocations_on_failure=(value)
- 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?
- Private: -
def display_matching_invocations_on_failure? @options[:display_matching_invocations_on_failure] end
def initialize(options = {})
- Private: -
def initialize(options = {}) @options = DEFAULTS.merge(options) end
def initialize_copy(other)
- Private: -
def initialize_copy(other) @options = other.options.dup end
def merge(other)
- Private: -
def merge(other) self.class.new(@options.merge(other.options)) end
def override(temporary_options)
- 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
- Private: -
def reset_configuration @configuration = nil end
def strict_keyword_argument_matching=(value)
- 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?
- Private: -
def strict_keyword_argument_matching? @options[:strict_keyword_argument_matching] end
def stubbing_method_on_nil
- Private: -
def stubbing_method_on_nil @options[:stubbing_method_on_nil] end
def stubbing_method_on_nil=(value)
-
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
- Private: -
def stubbing_method_on_non_mock_object @options[:stubbing_method_on_non_mock_object] end
def stubbing_method_on_non_mock_object=(value)
- 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
- Private: -
def stubbing_method_unnecessarily @options[:stubbing_method_unnecessarily] end
def stubbing_method_unnecessarily=(value)
- 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
- Private: -
def stubbing_non_existent_method @options[:stubbing_non_existent_method] end
def stubbing_non_existent_method=(value)
- 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
- Private: -
def stubbing_non_public_method @options[:stubbing_non_public_method] end
def stubbing_non_public_method=(value)
- 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)
- 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