Class: RSpec::Mocks::Configuration
Relationships & Source Files | |
Inherits: | Object |
Defined in: | rspec-mocks/lib/rspec/mocks/configuration.rb |
Overview
Provides configuration options for rspec-mocks.
Class Method Summary
- .new ⇒ Configuration constructor
Instance Attribute Summary
-
#allow_message_expectations_on_nil
rw
Sets whether
::RSpec
will warn, ignore, or fail a test when expectations are set on nil. -
#color=(value)
rw
Indicates whether or not diffs should be colored.
-
#color? ⇒ Boolean
rw
Indicates whether or not diffs should be colored.
-
#patch_marshal_to_support_partial_doubles=(val)
writeonly
Monkey-patch
Marshal.dump
to enable dumping of mocked or stubbed objects. -
#temporarily_suppress_partial_double_verification
rw
Internal use only
Internal use only
Used to track whether we are temporarily suppressing verifying partial doubles with ‘without_partial_double_verification { …
-
#transfer_nested_constants=(value)
rw
Sets the default for the
transfer_nested_constants
option when stubbing constants. - #transfer_nested_constants? ⇒ Boolean rw
-
#verify_doubled_constant_names=(value)
rw
When this is set to true, an error will be raised when
instance_double
orclass_double
is given the name of an undefined constant. - #verify_doubled_constant_names? ⇒ Boolean rw
-
#verify_partial_doubles=(val)
rw
When set to true, partial mocks will be verified the same as object doubles.
- #verify_partial_doubles? ⇒ Boolean rw
-
#yield_receiver_to_any_instance_implementation_blocks=(value)
rw
Sets whether or not
::RSpec
will yield the receiving instance of a message to blocks that are used for any_instance stub implementations. - #yield_receiver_to_any_instance_implementation_blocks? ⇒ Boolean rw
Instance Method Summary
-
#add_stub_and_should_receive_to(*modules)
Adds #stub and #should_receive to the given modules or classes.
-
#before_verifying_doubles(&block)
(also: #when_declaring_verifying_double)
Provides a way to perform customisations when verifying doubles.
-
#reset_syntaxes_to_default
Internal use only
Internal use only
Resets the configured syntax to the default.
-
#syntax
Returns an array with a list of syntaxes that are enabled.
-
#syntax=(*values)
Provides the ability to set either
expect
,should
or both syntaxes. -
#verifying_double_callbacks
Internal use only
Internal use only
Returns an array of blocks to call when verifying doubles.
-
#when_declaring_verifying_double(&block)
Alias for #before_verifying_doubles.
Constructor Details
.new ⇒ Configuration
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 5
def initialize @allow_message_expectations_on_nil = nil @yield_receiver_to_any_instance_implementation_blocks = true @verify_doubled_constant_names = false @transfer_nested_constants = false @verify_partial_doubles = false @temporarily_suppress_partial_double_verification = false @color = false end
Instance Attribute Details
#allow_message_expectations_on_nil (rw)
Sets whether ::RSpec
will warn, ignore, or fail a test when expectations are set on nil. By default, when this flag is not set, warning messages are issued when expectations are set on nil. This is to prevent false-positives and to catch potential bugs early on. When set to true
, warning messages are suppressed. When set to false
, it will raise an error.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 29
attr_accessor :
#color=(value) (rw)
Indicates whether or not diffs should be colored. Delegates to rspec-core’s color option if rspec-core is loaded; otherwise you can set it here.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 171
attr_writer :color
#color? ⇒ Boolean
(rw)
Indicates whether or not diffs should be colored. Delegates to rspec-core’s color option if rspec-core is loaded; otherwise you can set it here.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 176
def color? ::RSpec.configuration.color_enabled? end
#patch_marshal_to_support_partial_doubles=(val) (writeonly)
Monkey-patch Marshal.dump
to enable dumping of mocked or stubbed objects. By default this will not work since ::RSpec
mocks works by adding singleton methods that cannot be serialized. This patch removes these singleton methods before serialization. Setting to falsey removes the patch.
This method is idempotent.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 188
def patch_marshal_to_support_partial_doubles=(val) if val RSpec::Mocks::MarshalExtension.patch! else RSpec::Mocks::MarshalExtension.unpatch! end end
#temporarily_suppress_partial_double_verification (rw)
Used to track whether we are temporarily suppressing verifying partial doubles with ‘without_partial_double_verification { … }`
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 161
attr_accessor :temporarily_suppress_partial_double_verification
#transfer_nested_constants=(value) (rw)
Sets the default for the transfer_nested_constants
option when stubbing constants.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 145
attr_writer :transfer_nested_constants
#transfer_nested_constants? ⇒ Boolean
(rw)
[ GitHub ]
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 139
def transfer_nested_constants? !!@transfer_nested_constants end
#verify_doubled_constant_names=(value) (rw)
When this is set to true, an error will be raised when instance_double
or class_double
is given the name of an undefined constant. You probably only want to set this when running your entire test suite, with all production code loaded. Setting this for an isolated unit test will prevent you from being able to isolate it!
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 120
attr_writer :verify_doubled_constant_names
#verify_doubled_constant_names? ⇒ Boolean
(rw)
[ GitHub ]
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 111
def verify_doubled_constant_names? !!@verify_doubled_constant_names end
#verify_partial_doubles=(val) (rw)
When set to true, partial mocks will be verified the same as object doubles. Any stubs will have their arguments checked against the original method, and methods that do not exist cannot be stubbed.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 150
def verify_partial_doubles=(val) @verify_partial_doubles = !!val end
#verify_partial_doubles? ⇒ Boolean
(rw)
[ GitHub ]
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 154
def verify_partial_doubles? @verify_partial_doubles end
#yield_receiver_to_any_instance_implementation_blocks=(value) (rw)
Sets whether or not ::RSpec
will yield the receiving instance of a message to blocks that are used for any_instance stub implementations. When set, the first yielded argument will be the receiving instance. Defaults to true
.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 46
attr_writer :yield_receiver_to_any_instance_implementation_blocks
#yield_receiver_to_any_instance_implementation_blocks? ⇒ Boolean
(rw)
[ GitHub ]
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 31
def yield_receiver_to_any_instance_implementation_blocks? @yield_receiver_to_any_instance_implementation_blocks end
Instance Method Details
#add_stub_and_should_receive_to(*modules)
Adds #stub and #should_receive to the given modules or classes. This is usually only necessary if you application uses some proxy classes that “strip themselves down” to a bare minimum set of methods and remove #stub and #should_receive in the process.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 62
def add_stub_and_should_receive_to(*modules) modules.each do |mod| Syntax.enable_should(mod) end end
#before_verifying_doubles(&block) Also known as: #when_declaring_verifying_double
Provides a way to perform customisations when verifying doubles.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 128
def (&block) << block end
#reset_syntaxes_to_default
Resets the configured syntax to the default.
#syntax
Returns an array with a list of syntaxes that are enabled.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 104
def syntax syntaxes = [] syntaxes << :should if Syntax.should_enabled? syntaxes << :expect if Syntax.expect_enabled? syntaxes end
#syntax=(*values)
Provides the ability to set either expect
, should
or both syntaxes. ::RSpec
uses expect
syntax by default. This is needed if you want to explicitly enable should
syntax and/or explicitly disable expect
syntax.
end
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 81
def syntax=(*values) syntaxes = values.flatten if syntaxes.include?(:expect) Syntax.enable_expect else Syntax.disable_expect end if syntaxes.include?(:should) Syntax.enable_should else Syntax.disable_should end end
#verifying_double_callbacks
Returns an array of blocks to call when verifying doubles
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 135
def @verifying_double_callbacks ||= [] end
#when_declaring_verifying_double(&block)
Alias for #before_verifying_doubles.
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 131
alias : :