Module: RSpec::Mocks
- Defined in:
- lib/rspec/mocks.rb,
lib/rspec/mocks/mock.rb,
lib/rspec/mocks/proxy.rb,
lib/rspec/mocks/space.rb,
lib/rspec/mocks/syntax.rb,
lib/rspec/mocks/errors.rb,
lib/rspec/mocks/targets.rb,
lib/rspec/mocks/version.rb,
lib/rspec/mocks/stub_chain.rb,
lib/rspec/mocks/deprecation.rb,
lib/rspec/mocks/test_double.rb,
lib/rspec/mocks/order_group.rb,
lib/rspec/mocks/mutate_const.rb,
lib/rspec/mocks/method_double.rb,
lib/rspec/mocks/proxy_for_nil.rb,
lib/rspec/mocks/configuration.rb,
lib/rspec/mocks/error_generator.rb,
lib/rspec/mocks/example_methods.rb,
lib/rspec/mocks/matchers/receive.rb,
lib/rspec/mocks/argument_matchers.rb,
lib/rspec/mocks/any_instance/chain.rb,
lib/rspec/mocks/message_expectation.rb,
lib/rspec/mocks/any_instance/recorder.rb,
lib/rspec/mocks/argument_list_matcher.rb,
lib/rspec/mocks/matchers/have_received.rb,
lib/rspec/mocks/any_instance/stub_chain.rb,
lib/rspec/mocks/instance_method_stasher.rb,
lib/rspec/mocks/extensions/instance_exec.rb,
lib/rspec/mocks/any_instance/message_chains.rb,
lib/rspec/mocks/any_instance/stub_chain_chain.rb,
lib/rspec/mocks/any_instance/expectation_chain.rb
Defined Under Namespace
Modules: AnyInstance, ArgumentMatchers, Deprecation, ExampleMethods, Matchers, RecursiveConstMethods, Syntax, TestDouble, Version Classes: AllowanceTarget, AnyInstanceAllowanceTarget, AnyInstanceExpectationTarget, ArgumentListMatcher, Configuration, Constant, ConstantMutator, ExpectationTarget, MessageExpectation, Mock, Space, TargetBase
Constant Summary
- KERNEL_METHOD_METHOD =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
::Kernel.instance_method(:method)
- UnsupportedMatcherError =
Class.new(StandardError)
- NegationUnsupportedError =
Class.new(StandardError)
- ConstantStubber =
Keeps backwards compatibility since we had released an rspec-mocks that only supported stubbing. Later, we released the hide_const feature and decided that the term "mutator" was a better term to wrap up the concept of both stubbing and hiding.
ConstantMutator
Class Attribute Summary (collapse)
-
+ (Object) space
Returns the value of attribute space.
Class Method Summary (collapse)
-
+ (Object) allow_message(subject, message, opts = {})
Adds an allowance (stub) on
subject
. - + (Object) any_instance_recorder_for(klass)
- + (Object) configuration
-
+ (Object) expect_message(subject, message, opts = {})
Sets a message expectation on
subject
. -
+ (Object) method_handle_for(object, method_name)
private
Used internally to get a method handle for a particular object and method name.
- + (Object) proxies_of(klass)
- + (Object) proxy_for(object)
- + (Object) setup(host)
- + (Object) teardown
- + (Object) verify
Instance Method Summary (collapse)
-
- (Object) allow
Used to wrap an object in preparation for stubbing a method on it.
-
- (Object) allow_any_instance_of
Used to wrap a class in preparation for stubbing a method on instances of it.
-
- (Recorder) any_instance
Used to set stubs and message expectations on any instance of a given class.
-
- (Object) as_null_object
Tells the object to respond to all messages.
-
- (Object) expect
Used to wrap an object in preparation for setting a mock expectation on it.
-
- (Object) expect_any_instance_of
Used to wrap a class in preparation for setting a mock expectation on instances of it.
-
- (Object) null_object?
Returns true if this object has received
as_null_object
. -
- (Object) receive
Used to specify a message that you expect or allow an object to receive.
-
- (Object) should_not_receive
Sets and expectation that this object should not receive a message during this example.
-
- (Object) should_receive
Sets an expectation that this object should receive a message before the end of the example.
-
- (Object) stub
Tells the object to respond to the message with the specified value.
-
- (Object) stub_chain
Stubs a chain of methods.
-
- (Object) unstub
Removes a stub.
Class Attribute Details
+ (Object) space
Returns the value of attribute space
8 9 10 |
# File 'lib/rspec/mocks.rb', line 8 def space @space end |
Class Method Details
+ (Object) allow_message(subject, message, opts = {})
Adds an allowance (stub) on subject
49 50 51 52 53 |
# File 'lib/rspec/mocks.rb', line 49 def (subject, , opts={}, &block) orig_caller = opts.fetch(:expected_from) { caller(1)[0] } ::RSpec::Mocks.proxy_for(subject). add_stub(orig_caller, .to_sym, opts, &block) end |
+ (Object) any_instance_recorder_for(klass)
33 34 35 |
# File 'lib/rspec/mocks.rb', line 33 def any_instance_recorder_for(klass) space.any_instance_recorder_for(klass) end |
+ (Object) configuration
48 49 50 |
# File 'lib/rspec/mocks/configuration.rb', line 48 def self.configuration @configuration ||= Configuration.new end |
+ (Object) expect_message(subject, message, opts = {})
Sets a message expectation on subject
.
66 67 68 69 70 |
# File 'lib/rspec/mocks.rb', line 66 def (subject, , opts={}, &block) orig_caller = opts.fetch(:expected_from) { caller(1)[0] } ::RSpec::Mocks.proxy_for(subject). (orig_caller, .to_sym, opts, &block) end |
+ (Object) method_handle_for(object, method_name)
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.
Used internally to get a method handle for a particular object and method name.
Includes handling for a few special cases:
- Objects that redefine #method (e.g. an HTTPRequest struct)
- BasicObject subclasses that mixin a Kernel dup (e.g. SimpleDelegator)
83 84 85 86 87 88 89 |
# File 'lib/rspec/mocks.rb', line 83 def method_handle_for(object, method_name) if ::Kernel === object KERNEL_METHOD_METHOD.bind(object).call(method_name) else object.method(method_name) end end |
+ (Object) proxies_of(klass)
29 30 31 |
# File 'lib/rspec/mocks.rb', line 29 def proxies_of(klass) space.proxies_of(klass) end |
+ (Object) proxy_for(object)
25 26 27 |
# File 'lib/rspec/mocks.rb', line 25 def proxy_for(object) space.proxy_for(object) end |
+ (Object) setup(host)
10 11 12 13 14 15 |
# File 'lib/rspec/mocks.rb', line 10 def setup(host) (class << host; self; end).class_eval do include RSpec::Mocks::ExampleMethods end self.space ||= RSpec::Mocks::Space.new end |
+ (Object) teardown
21 22 23 |
# File 'lib/rspec/mocks.rb', line 21 def teardown space.reset_all end |
+ (Object) verify
17 18 19 |
# File 'lib/rspec/mocks.rb', line 17 def verify space.verify_all end |
Instance Method Details
- (Object) allow
This is only available when you have enabled the expect
syntax.
Used to wrap an object in preparation for stubbing a method on it.
|
# File 'lib/rspec/mocks/syntax.rb', line 304
|
- (Object) 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.
|
# File 'lib/rspec/mocks/syntax.rb', line 324
|
- (Recorder) any_instance
This is only available when you have enabled the should
syntax.
Used to set stubs and message expectations on any instance of a given
class. Returns a Recorder, which records messages like
stub
and should_receive
for later playback on instances of the
class.
|
# File 'lib/rspec/mocks/syntax.rb', line 271
|
- (Object) as_null_object
This is only available when you have enabled the should
syntax.
Tells the object to respond to all messages. If specific stub values are declared, they'll work as expected. If not, the receiver is returned.
|
# File 'lib/rspec/mocks/syntax.rb', line 259
|
- (Object) expect
This method is usually provided by rspec-expectations, unless
you are using rspec-mocks w/o rspec-expectations, in which case it
is only made available if you enable the expect
syntax.
Used to wrap an object in preparation for setting a mock expectation on it.
|
# File 'lib/rspec/mocks/syntax.rb', line 292
|
- (Object) expect_any_instance_of
This is only available when you have enabled the expect
syntax.
Used to wrap a class in preparation for setting a mock expectation on instances of it.
|
# File 'lib/rspec/mocks/syntax.rb', line 314
|
- (Object) null_object?
This is only available when you have enabled the should
syntax.
Returns true if this object has received as_null_object
|
# File 'lib/rspec/mocks/syntax.rb', line 266
|
- (Object) receive
This is only available when you have enabled the expect
syntax.
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.
|
# File 'lib/rspec/mocks/syntax.rb', line 334
|
- (Object) should_not_receive
Sets and expectation that this object should not receive a message during this example.
|
# File 'lib/rspec/mocks/syntax.rb', line 202
|
- (Object) should_receive
This is only available when you have enabled the should
syntax.
Sets an expectation that this object should receive a message before the end of the example.
|
# File 'lib/rspec/mocks/syntax.rb', line 189
|
- (Object) stub
This is only available when you have enabled the should
syntax.
Tells the object to respond to the message with the specified value.
|
# File 'lib/rspec/mocks/syntax.rb', line 206
|
- (Object) stub_chain(method1, method2) - (Object) stub_chain("method1.method2") - (Object) stub_chain(method1, method_to_value_hash)
This is only available when you have enabled the should
syntax.
Stubs a chain of methods.
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 stub_chain
a code smell. Even though not all code smells
indicate real problems (think fluent interfaces), stub_chain
still
results in brittle examples. For example, if you write
foo.stub_chain(:bar, :baz => 37)
in a spec and then the
implementation calls foo.baz.bar
, the stub will not work.
|
# File 'lib/rspec/mocks/syntax.rb', line 228
|
- (Object) unstub
This is only available when you have enabled the should
syntax.
Removes a stub. On a double, the object will no longer respond to
message
. On a real object, the original method (if it exists) is
restored.
This is rarely used, but can be useful when a stub is set up during a
shared before
hook for the common case, but you want to replace it
for a special case.
|
# File 'lib/rspec/mocks/syntax.rb', line 217
|