123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Mocks

Relationships & Source Files
Namespace Children
Modules:
Classes:
Defined in: rspec-mocks/lib/rspec/mocks.rb,
rspec-mocks/lib/rspec/mocks/argument_list_matcher.rb,
rspec-mocks/lib/rspec/mocks/argument_matchers.rb,
rspec-mocks/lib/rspec/mocks/configuration.rb,
rspec-mocks/lib/rspec/mocks/error_generator.rb,
rspec-mocks/lib/rspec/mocks/example_methods.rb,
rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb,
rspec-mocks/lib/rspec/mocks/marshal_extension.rb,
rspec-mocks/lib/rspec/mocks/message_chain.rb,
rspec-mocks/lib/rspec/mocks/message_expectation.rb,
rspec-mocks/lib/rspec/mocks/method_double.rb,
rspec-mocks/lib/rspec/mocks/method_reference.rb,
rspec-mocks/lib/rspec/mocks/minitest_integration.rb,
rspec-mocks/lib/rspec/mocks/minitest_integration.rb,
rspec-mocks/lib/rspec/mocks/mutate_const.rb,
rspec-mocks/lib/rspec/mocks/object_reference.rb,
rspec-mocks/lib/rspec/mocks/order_group.rb,
rspec-mocks/lib/rspec/mocks/proxy.rb,
rspec-mocks/lib/rspec/mocks/space.rb,
rspec-mocks/lib/rspec/mocks/syntax.rb,
rspec-mocks/lib/rspec/mocks/targets.rb,
rspec-mocks/lib/rspec/mocks/test_double.rb,
rspec-mocks/lib/rspec/mocks/verifying_double.rb,
rspec-mocks/lib/rspec/mocks/verifying_message_expectation.rb,
rspec-mocks/lib/rspec/mocks/verifying_proxy.rb,
rspec-mocks/lib/rspec/mocks/version.rb,
rspec-mocks/lib/rspec/mocks/any_instance/chain.rb,
rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb,
rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb,
rspec-mocks/lib/rspec/mocks/any_instance/expectation_chain.rb,
rspec-mocks/lib/rspec/mocks/any_instance/message_chains.rb,
rspec-mocks/lib/rspec/mocks/any_instance/proxy.rb,
rspec-mocks/lib/rspec/mocks/any_instance/recorder.rb,
rspec-mocks/lib/rspec/mocks/any_instance/stub_chain.rb,
rspec-mocks/lib/rspec/mocks/any_instance/stub_chain_chain.rb,
rspec-mocks/lib/rspec/mocks/matchers/expectation_customization.rb,
rspec-mocks/lib/rspec/mocks/matchers/have_received.rb,
rspec-mocks/lib/rspec/mocks/matchers/receive.rb,
rspec-mocks/lib/rspec/mocks/matchers/receive_message_chain.rb,
rspec-mocks/lib/rspec/mocks/matchers/receive_messages.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).

Constant Summary

Class Attribute Summary

  • .space readonly Internal use only Internal use only

Class Method Summary

Class Attribute Details

.space (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks.rb', line 106

attr_reader :space

Class Method Details

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

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

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks.rb', line 69

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

.configuration

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

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/configuration.rb', line 206

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

.error_generator

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/error_generator.rb', line 386

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

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

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

[ GitHub ]

  
# File 'rspec-mocks/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

.setup

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

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks.rb', line 38

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

.teardown

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.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks.rb', line 51

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

.verify

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

[ GitHub ]

  
# File 'rspec-mocks/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

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks.rb', line 92

def self.with_temporary_scope
  setup

  begin
    result = yield
    verify
    result
  ensure
    teardown
  end
end