Class: RSpec::Mocks::Configuration
- Inherits:
-
Object
- Object
- RSpec::Mocks::Configuration
- Defined in:
- lib/rspec/mocks/configuration.rb
Overview
Provides configuration options for rspec-mocks.
Instance Attribute Summary (collapse)
-
- (Object) allow_message_expectations_on_nil
Sets whether RSpec will warn, ignore, or fail a test when expectations are set on nil.
-
- (Object) color
writeonly
Indicates whether or not diffs should be colored.
-
- (Object) transfer_nested_constants
writeonly
Sets the default for the
transfer_nested_constants
option when stubbing constants. -
- (Object) verify_doubled_constant_names
writeonly
When this is set to true, an error will be raised when
instance_double
orclass_double
is given the name of an undefined constant. -
- (Object) yield_receiver_to_any_instance_implementation_blocks
writeonly
Sets whether or not RSpec will yield the receiving instance of a message to blocks that are used for any_instance stub implementations.
Instance Method Summary (collapse)
-
- (Object) add_stub_and_should_receive_to(*modules)
Adds
stub
andshould_receive
to the given modules or classes. -
- (Object) before_verifying_doubles(&block)
(also: #when_declaring_verifying_double)
Provides a way to perform customisations when verifying doubles.
-
- (Boolean) color?
Indicates whether or not diffs should be colored.
-
- (Configuration) initialize
constructor
A new instance of Configuration.
-
- (Object) patch_marshal_to_support_partial_doubles=(val)
Monkey-patch
Marshal.dump
to enable dumping of mocked or stubbed objects. -
- (Object) reset_syntaxes_to_default
private
Resets the configured syntax to the default.
-
- (Object) syntax
Returns an array with a list of syntaxes that are enabled.
-
- (Object) syntax=(*values)
Provides the ability to set either
expect
,should
or both syntaxes. - - (Boolean) transfer_nested_constants?
- - (Boolean) verify_doubled_constant_names?
-
- (Object) verify_partial_doubles=(val)
When set to true, partial mocks will be verified the same as object doubles.
- - (Boolean) verify_partial_doubles?
-
- (Object) verifying_double_callbacks
private
Returns an array of blocks to call when verifying doubles.
- - (Boolean) yield_receiver_to_any_instance_implementation_blocks?
Constructor Details
- (Configuration) initialize
Returns a new instance of Configuration
5 6 7 8 9 10 11 |
# File '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 end |
Instance Attribute Details
- (Object) allow_message_expectations_on_nil
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.
27 28 29 |
# File 'lib/rspec/mocks/configuration.rb', line 27 def @allow_message_expectations_on_nil end |
- (Object) color=(value) (writeonly)
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.
164 165 166 |
# File 'lib/rspec/mocks/configuration.rb', line 164 def color=(value) @color = value end |
- (Object) transfer_nested_constants=(value) (writeonly)
Sets the default for the transfer_nested_constants
option when
stubbing constants.
143 144 145 |
# File 'lib/rspec/mocks/configuration.rb', line 143 def transfer_nested_constants=(value) @transfer_nested_constants = value end |
- (Object) verify_doubled_constant_names=(value) (writeonly)
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!
118 119 120 |
# File 'lib/rspec/mocks/configuration.rb', line 118 def verify_doubled_constant_names=(value) @verify_doubled_constant_names = value end |
- (Object) yield_receiver_to_any_instance_implementation_blocks=(value) (writeonly)
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
.
44 45 46 |
# File 'lib/rspec/mocks/configuration.rb', line 44 def yield_receiver_to_any_instance_implementation_blocks=(value) @yield_receiver_to_any_instance_implementation_blocks = value end |
Instance Method Details
- (Object) 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.
60 61 62 63 64 |
# File 'lib/rspec/mocks/configuration.rb', line 60 def add_stub_and_should_receive_to(*modules) modules.each do |mod| Syntax.enable_should(mod) end end |
- (Object) before_verifying_doubles(&block) Also known as: when_declaring_verifying_double
Provides a way to perform customisations when verifying doubles.
126 127 128 |
# File 'lib/rspec/mocks/configuration.rb', line 126 def (&block) << block end |
- (Boolean) color?
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.
169 170 171 |
# File 'lib/rspec/mocks/configuration.rb', line 169 def color? ::RSpec.configuration.color_enabled? end |
- (Object) patch_marshal_to_support_partial_doubles=(val)
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.
181 182 183 184 185 186 187 |
# File 'lib/rspec/mocks/configuration.rb', line 181 def patch_marshal_to_support_partial_doubles=(val) if val RSpec::Mocks::MarshalExtension.patch! else RSpec::Mocks::MarshalExtension.unpatch! end end |
- (Object) reset_syntaxes_to_default
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resets the configured syntax to the default.
191 192 193 194 |
# File 'lib/rspec/mocks/configuration.rb', line 191 def reset_syntaxes_to_default self.syntax = [:should, :expect] RSpec::Mocks::Syntax.warn_about_should! end |
- (Object) syntax
Returns an array with a list of syntaxes that are enabled.
102 103 104 105 106 107 |
# File 'lib/rspec/mocks/configuration.rb', line 102 def syntax syntaxes = [] syntaxes << :should if Syntax.should_enabled? syntaxes << :expect if Syntax.expect_enabled? syntaxes end |
- (Object) 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rspec/mocks/configuration.rb', line 79 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 |
- (Boolean) transfer_nested_constants?
137 138 139 |
# File 'lib/rspec/mocks/configuration.rb', line 137 def transfer_nested_constants? !!@transfer_nested_constants end |
- (Boolean) verify_doubled_constant_names?
109 110 111 |
# File 'lib/rspec/mocks/configuration.rb', line 109 def verify_doubled_constant_names? !!@verify_doubled_constant_names end |
- (Object) verify_partial_doubles=(val)
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.
148 149 150 |
# File 'lib/rspec/mocks/configuration.rb', line 148 def verify_partial_doubles=(val) @verify_partial_doubles = !!val end |
- (Boolean) verify_partial_doubles?
152 153 154 |
# File 'lib/rspec/mocks/configuration.rb', line 152 def verify_partial_doubles? @verify_partial_doubles end |
- (Object) verifying_double_callbacks
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an array of blocks to call when verifying doubles
133 134 135 |
# File 'lib/rspec/mocks/configuration.rb', line 133 def @verifying_double_callbacks ||= [] end |
- (Boolean) yield_receiver_to_any_instance_implementation_blocks?
29 30 31 |
# File 'lib/rspec/mocks/configuration.rb', line 29 def yield_receiver_to_any_instance_implementation_blocks? @yield_receiver_to_any_instance_implementation_blocks end |