Module: RSpec::Mocks

Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/proxy.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/space.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/syntax.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/targets.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/version.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/order_group.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/configuration.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/message_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_double.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/error_generator.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/example_methods.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/verifying_proxy.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/matchers/receive.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/object_reference.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/verifying_double.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_matchers.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/marshal_extension.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/proxy.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/message_expectation.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/minitest_integration.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/minitest_integration.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/recorder.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/argument_list_matcher.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/matchers/have_received.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/stub_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/instance_method_stasher.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/matchers/receive_messages.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/message_chains.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/error_generator.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/stub_chain_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/verifying_message_expectation.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/expectation_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/matchers/receive_message_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/any_instance/expect_chain_chain.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/matchers/expectation_customization.rb

Overview

Contains top-level utility methods. While this contains a few public methods, these are not generally meant to be called from a test or example. They exist primarily for integration with test frameworks (such as rspec-core).

Defined Under Namespace

Modules: AnyInstance, ArgumentMatchers, ExampleMethods, ExpectationTargetMethods, Matchers, MinitestIntegration, ObjectVerifyingDoubleMethods, PartialClassDoubleProxyMethods, Syntax, TargetDelegationClassMethods, TargetDelegationInstanceMethods, TestDouble, TestDoubleFormatter, VerifyingDouble, VerifyingProxyMethods, Version Classes: AllowanceTarget, AndInvokeImplementation, AndReturnImplementation, AndWrapOriginalImplementation, AndYieldImplementation, AnyInstanceAllowanceTarget, AnyInstanceExpectationTarget, ArgumentListMatcher, CallbackInvocationStrategy, ClassNewMethodReference, ClassVerifyingDouble, Configuration, Constant, ConstantMutator, DirectObjectReference, Double, ErrorGenerator, ExpectChain, ExpectationTarget, Implementation, InstanceMethodReference, InstanceMethodStasher, InstanceVerifyingDouble, MarshalExtension, MessageChain, MessageExpectation, MethodDouble, MethodReference, NamedObjectReference, NestedSpace, NoCallbackInvocationStrategy, ObjectMethodReference, ObjectReference, ObjectVerifyingDouble, OrderGroup, PartialClassDoubleProxy, PartialDoubleProxy, Proxy, ProxyForNil, RootSpace, SimpleMessageExpectation, Space, StubChain, TargetBase, TestDoubleProxy, VerifyingExistingClassNewMethodDouble, VerifyingExistingMethodDouble, VerifyingMessageExpectation, VerifyingMethodDouble, VerifyingPartialClassDoubleProxy, VerifyingPartialDoubleProxy, VerifyingProxy

Constant Summary collapse

IGNORED_BACKTRACE_LINE =
'this backtrace line is ignored'
MockExpectationError =

Raised when a message expectation is not satisfied.

::Minitest::Assertion
ExpiredTestDoubleError =

Raised when a test double is used after it has been torn down (typically at the end of an rspec-core example).

Class.new(MockExpectationError)
OutsideOfExampleError =

Raised when doubles or partial doubles are used outside of the per-test lifecycle.

Class.new(StandardError)
MockExpectationAlreadyInvokedError =

Raised when an expectation customization method (e.g. ‘with`, `and_return`) is called on a message expectation which has already been invoked.

Class.new(Exception)
CannotSupportArgMutationsError =
Deprecated.

We no longer raise this error but the constant remains until RSpec 4 for SemVer reasons.

Raised for situations that RSpec cannot support due to mutations made externally on arguments that RSpec is holding onto to use for later comparisons.

Class.new(StandardError)
UnsupportedMatcherError =
Class.new(StandardError)
NegationUnsupportedError =
Class.new(StandardError)
VerifyingDoubleNotDefinedError =
Class.new(StandardError)
DEFAULT_CALLBACK_INVOCATION_STRATEGY =
CallbackInvocationStrategy.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.spaceObject (readonly)



106
107
108
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 106

def space
  @space
end

Class Method Details

.allow_message(subject, message, opts = {}) { ... } ⇒ Object

Adds an allowance (stub) on ‘subject`

Examples:

Defines the implementation of ‘foo` on `bar`, using the passed block

x = 0
RSpec::Mocks.allow_message(bar, :foo) { x += 1 }

Parameters:

  • subject

    the subject to which the message will be added

  • message

    a symbol, representing the message that will be added.

  • opts (defaults to: {})

    a hash of options, :expected_from is used to set the original call site

Yields:

  • an optional implementation for the allowance



69
70
71
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 69

def self.allow_message(subject, message, opts={}, &block)
  space.proxy_for(subject).add_stub(message, opts, &block)
end

.configurationObject

Mocks specific configuration, as distinct from ‘RSpec.configuration` which is core RSpec configuration.



206
207
208
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/configuration.rb', line 206

def self.configuration
  @configuration ||= Configuration.new
end

.error_generatorObject



386
387
388
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/error_generator.rb', line 386

def self.error_generator
  @error_generator ||= ErrorGenerator.new
end

.expect_message(subject, message, opts = {}) { ... } ⇒ Object

Sets a message expectation on ‘subject`.

Examples:

Expect the message ‘foo` to receive `bar`, then call it

RSpec::Mocks.expect_message(bar, :foo)
bar.foo

Parameters:

  • subject

    the subject on which the message will be expected

  • message

    a symbol, representing the message that will be expected.

  • opts (defaults to: {})

    a hash of options, :expected_from is used to set the original call site

Yields:

  • an optional implementation for the expectation



84
85
86
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 84

def self.expect_message(subject, message, opts={}, &block)
  space.proxy_for(subject).add_message_expectation(message, opts, &block)
end

.setupObject

Performs per-test/example setup. This should be called before an test or example begins.



38
39
40
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 38

def self.setup
  @space_stack << (@space = space.new_scope)
end

.teardownObject

Cleans up all test double state (including any methods that were redefined on partial doubles). This must be called after each example, even if an error was raised during the example.



51
52
53
54
55
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 51

def self.teardown
  space.reset_all
  @space_stack.pop
  @space = @space_stack.last || @root_space
end

.verifyObject

Verifies any message expectations that were set during the test or example. This should be called at the end of an example.



44
45
46
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 44

def self.verify
  space.verify_all
end

.with_temporary_scopeObject

Call the passed block and verify mocks after it has executed. This allows mock usage in arbitrary places, such as a ‘before(:all)` hook.

Returns:

  • (Object)

    the return value from the block



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks.rb', line 92

def self.with_temporary_scope
  setup

  begin
    result = yield
    verify
    result
  ensure
    teardown
  end
end