Module: RSpec::Mocks::ExampleMethods
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
ArgumentMatchers
|
|
Defined in: | rspec-mocks/lib/rspec/mocks/example_methods.rb |
Overview
Contains methods intended to be used from within code examples. Mix this in to your test context (such as a test framework base class) to use rspec-mocks with your test framework. If you’re using rspec-core, it’ll take care of doing this for you.
Class Method Summary
- .declare_double(type, *args) Internal use only Internal use only
- .declare_verifying_double(type, ref, *args) Internal use only Internal use only
- .extended(object) Internal use only Internal use only
- .included(klass) Internal use only Internal use only
Instance Method Summary
-
#allow
Used to wrap an object in preparation for stubbing a method on it.
-
#allow_any_instance_of
Used to wrap a class in preparation for stubbing a method on instances of it.
-
#allow_message_expectations_on_nil
deprecated
Deprecated.
Use Configuration#allow_message_expectations_on_nil instead.
-
#class_double(doubled_class) ⇒ Object
Constructs a test double against a specific class.
-
#class_spy(doubled_class) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific class.
-
#double ⇒ Double
Constructs an instance of [RSpec::Mocks::Double](RSpec::Mocks::Double) configured with an optional name, used for reporting in failure messages, and an optional hash of message/return-value pairs.
-
#expect
Used to wrap an object in preparation for setting a mock expectation on it.
-
#expect_any_instance_of
Used to wrap a class in preparation for setting a mock expectation on instances of it.
-
#have_received(method_name, &block)
Verifies that the given object received the expected message during the course of the test.
-
#hide_const(constant_name)
Hides the named constant with the given value.
-
#instance_double(doubled_class) ⇒ Object
Constructs a test double against a specific class.
-
#instance_spy(doubled_class) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific class.
-
#object_double(object_or_name) ⇒ Object
Constructs a test double against a specific object.
-
#object_spy(object_or_name) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific object.
-
#receive
Used to specify a message that you expect or allow an object to receive.
-
#receive_message_chain(method1, method2)
stubs/mocks a chain of messages on an object or test double.
-
#receive_messages
Shorthand syntax used to setup message(s), and their return value(s), that you expect or allow an object to receive.
-
#spy ⇒ Double
Constructs a test double that is optimized for use with #have_received.
-
#stub_const(constant_name, value, options = {}) ⇒ Object
Stubs the named constant with the given value.
-
#without_partial_double_verification
Turns off the verifying of partial doubles for the duration of the block, this is useful in situations where methods are defined at run time and you wish to define stubs for them but not turn off partial doubles for the entire run suite.
ArgumentMatchers
- Included
#a_kind_of | Alias for ArgumentMatchers#kind_of. |
#an_instance_of | Alias for ArgumentMatchers#instance_of. |
#any_args | Acts like an arg splat, matching any number of args at any point in an arg list. |
#anything | Matches any argument at all. |
#array_excluding | Matches an array that excludes the specified items. |
#array_including | Matches an array that includes the specified items at least once. |
#boolean | Matches a boolean value. |
#duck_type | Matches if the actual argument responds to the specified messages. |
#hash_excluding | Matches a hash that doesn’t include the specified key(s) or key/value. |
#hash_including | Matches a hash that includes the specified key(s) or key/value pairs. |
#hash_not_including | Alias for ArgumentMatchers#hash_excluding. |
#instance_of | Matches if ‘arg.instance_of?(klass)`. |
#kind_of | Matches if ‘arg.kind_of?(klass)`. |
#no_args | Matches no arguments. |
Class Method Details
.declare_double(type, *args)
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 423
def self.declare_double(type, *args) args << {} unless Hash === args.last type.new(*args) end
.declare_verifying_double(type, ref, *args)
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 408
def self. (type, ref, *args) if RSpec::Mocks.configuration.verify_doubled_constant_names? && !ref.defined? RSpec::Mocks.error_generator. (ref) end RSpec::Mocks.configuration. .each do |block| block.call(ref) end declare_double(type, ref, *args) end
.extended(object)
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 401
def self.extended(object) # This gets extended in so that if `RSpec::Matchers` is included in # `klass` later, its definition of `expect` will take precedence. object.extend ExpectHost unless object.respond_to?(:expect) end
.included(klass)
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 392
def self.included(klass) klass.class_exec do # This gets mixed in so that if `RSpec::Matchers` is included in # `klass` later, its definition of `expect` will take precedence. include ExpectHost unless method_defined?(:expect) end end
Instance Method Details
#allow
If you disable the :expect
syntax this method will be undefined.
Used to wrap an object in preparation for stubbing a method on it.
#allow_any_instance_of
This is only available when you have enabled the #expect syntax.
Used to wrap a class in preparation for stubbing a method on instances of it.
#allow_message_expectations_on_nil
Use Configuration#allow_message_expectations_on_nil instead.
Disables warning messages about expectations being set on nil.
By default warning messages are issued when expectations are set on nil. This is to prevent false-positives and to catch potential bugs early on.
#class_double(doubled_class) ⇒ Object
#class_double(doubled_class, name) ⇒ Object
#class_double(doubled_class, stubs) ⇒ Object
#class_double(doubled_class, name, stubs) ⇒ Object
Object
#class_double(doubled_class, name) ⇒ Object
#class_double(doubled_class, stubs) ⇒ Object
#class_double(doubled_class, name, stubs) ⇒ Object
Constructs a test double against a specific class. If the given class name has been loaded, only class methods defined on the class are allowed to be stubbed. In all other ways it behaves like a [double](double).
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 79
def class_double(doubled_class, *args) ref = ObjectReference.for(doubled_class) ExampleMethods. (ClassVerifyingDouble, ref, *args) end
#class_spy(doubled_class) ⇒ Object
#class_spy(doubled_class, name) ⇒ Object
#class_spy(doubled_class, stubs) ⇒ Object
#class_spy(doubled_class, name, stubs) ⇒ Object
Object
#class_spy(doubled_class, name) ⇒ Object
#class_spy(doubled_class, stubs) ⇒ Object
#class_spy(doubled_class, name, stubs) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific class. If the given class name has been loaded, only class methods defined on the class are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An class_spy automatically spies on all class methods to which the class responds.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 191
def class_spy(*args) class_double(*args).as_null_object end
Constructs an instance of [RSpec::Mocks::Double](RSpec::Mocks::Double) configured with an optional name, used for reporting in failure messages, and an optional hash of message/return-value pairs.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 34
def double(*args) ExampleMethods.declare_double(Double, *args) end
#expect
This method is usually provided by rspec-expectations. However, if you use rspec-mocks without rspec-expectations, there’s a definition of it that is made available here. If you disable the :expect
syntax this method will be undefined.
Used to wrap an object in preparation for setting a mock expectation on it.
#expect_any_instance_of
If you disable the :expect
syntax this method will be undefined.
Used to wrap a class in preparation for setting a mock expectation on instances of it.
#have_received(method_name, &block)
‘have_received(…).with(…)` is unable to work properly when passed arguments are mutated after the spy records the received message.
Verifies that the given object received the expected message during the course of the test. On a spy objects or as null object doubles this works for any method, on other objects the method must have been stubbed beforehand in order for messages to be verified.
Stubbing and verifying messages received in this way implements the Test Spy pattern.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 281
def have_received(method_name, &block) Matchers::HaveReceived.new(method_name, &block) end
#hide_const(constant_name)
Hides the named constant with the given value. The constant will be undefined for the duration of the test.
Like method stubs, the constant will be restored to its original value when the example completes.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 256
def hide_const(constant_name) ConstantMutator.hide(constant_name) end
#instance_double(doubled_class) ⇒ Object
#instance_double(doubled_class, name) ⇒ Object
#instance_double(doubled_class, stubs) ⇒ Object
#instance_double(doubled_class, name, stubs) ⇒ Object
Object
#instance_double(doubled_class, name) ⇒ Object
#instance_double(doubled_class, stubs) ⇒ Object
#instance_double(doubled_class, name, stubs) ⇒ Object
Constructs a test double against a specific class. If the given class name has been loaded, only instance methods defined on the class are allowed to be stubbed. In all other ways it behaves like a [double](double).
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 56
def instance_double(doubled_class, *args) ref = ObjectReference.for(doubled_class) ExampleMethods. (InstanceVerifyingDouble, ref, *args) end
#instance_spy(doubled_class) ⇒ Object
#instance_spy(doubled_class, name) ⇒ Object
#instance_spy(doubled_class, stubs) ⇒ Object
#instance_spy(doubled_class, name, stubs) ⇒ Object
Object
#instance_spy(doubled_class, name) ⇒ Object
#instance_spy(doubled_class, stubs) ⇒ Object
#instance_spy(doubled_class, name, stubs) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific class. If the given class name has been loaded, only instance methods defined on the class are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An instance_spy automatically spies on all instance methods to which the class responds.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 144
def instance_spy(*args) instance_double(*args).as_null_object end
#object_double(object_or_name) ⇒ Object
#object_double(object_or_name, name) ⇒ Object
#object_double(object_or_name, stubs) ⇒ Object
#object_double(object_or_name, name, stubs) ⇒ Object
Object
#object_double(object_or_name, name) ⇒ Object
#object_double(object_or_name, stubs) ⇒ Object
#object_double(object_or_name, name, stubs) ⇒ Object
Constructs a test double against a specific object. Only the methods the object responds to are allowed to be stubbed. If a String argument is provided, it is assumed to reference a constant object which is used for verification. In all other ways it behaves like a [double](double).
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 102
def object_double(object_or_name, *args) ref = ObjectReference.for(object_or_name, :allow_direct_object_refs) ExampleMethods. (ObjectVerifyingDouble, ref, *args) end
#object_spy(object_or_name) ⇒ Object
#object_spy(object_or_name, name) ⇒ Object
#object_spy(object_or_name, stubs) ⇒ Object
#object_spy(object_or_name, name, stubs) ⇒ Object
Object
#object_spy(object_or_name, name) ⇒ Object
#object_spy(object_or_name, stubs) ⇒ Object
#object_spy(object_or_name, name, stubs) ⇒ Object
Constructs a test double that is optimized for use with #have_received against a specific object. Only instance methods defined on the object are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An object_spy automatically spies on all methods to which the object responds.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 167
def object_spy(*args) object_double(*args).as_null_object end
#receive
If you disable the :expect
syntax this method will be undefined.
Used to specify a message that you expect or allow an object to receive. The object returned by receive
supports the same fluent interface that #should_receive and #stub have always supported, allowing you to constrain the arguments or number of times, and configure how the object should respond to the message.
#receive_message_chain(method1, method2)
#receive_message_chain("method1.method2")
#receive_message_chain(method1, method_to_value_hash)
If you disable the :expect
syntax this method will be undefined.
stubs/mocks a chain of messages on an object or test double.
## Warning:
Chains can be arbitrarily long, which makes it quite painless to violate the Law of Demeter in violent ways, so you should consider any use of receive_message_chain
a code smell. Even though not all code smells indicate real problems (think fluent interfaces), receive_message_chain
still results in brittle examples. For example, if you write ‘allow(foo).to receive_message_chain(:bar, :baz
=> 37)` in a spec and then the implementation calls foo.baz.bar
, the stub will not work.
#receive_messages
If you disable the :expect
syntax this method will be undefined.
Shorthand syntax used to setup message(s), and their return value(s), that you expect or allow an object to receive. The method takes a hash of messages and their respective return values. Unlike with #receive, you cannot apply further customizations using a block or the fluent interface.
Constructs a test double that is optimized for use with #have_received. With a normal double one has to stub methods in order to be able to spy them. A spy automatically spies on all methods.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 120
def spy(*args) double(*args).as_null_object end
#stub_const(constant_name, value, options = {}) ⇒ Object
Stubs the named constant with the given value. Like method stubs, the constant will be restored to its original value (or lack of one, if it was undefined) when the example completes.
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 241
def stub_const(constant_name, value, ={}) ConstantMutator.stub(constant_name, value, ) end
#without_partial_double_verification
Turns off the verifying of partial doubles for the duration of the block, this is useful in situations where methods are defined at run time and you wish to define stubs for them but not turn off partial doubles for the entire run suite. (e.g. view specs in rspec-rails).
# File 'rspec-mocks/lib/rspec/mocks/example_methods.rb', line 289
def without_partial_double_verification original_state = Mocks.configuration.temporarily_suppress_partial_double_verification Mocks.configuration.temporarily_suppress_partial_double_verification = true yield ensure Mocks.configuration.temporarily_suppress_partial_double_verification = original_state end