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/errors.rb,
lib/rspec/mocks/syntax.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/order_group.rb,
lib/rspec/mocks/test_double.rb,
lib/rspec/mocks/mutate_const.rb,
lib/rspec/mocks/configuration.rb,
lib/rspec/mocks/method_double.rb,
lib/rspec/mocks/proxy_for_nil.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, Double, ExpectationTarget, MessageExpectation, Space, TargetBase, TestDoubleProxy

Constant Summary collapse

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)
DEPRECATED_CONSTANTS =
{
  :Mock            => Double,
  :ConstantStubber => ConstantMutator,
}
UnsupportedMatcherError =
Class.new(StandardError)
NegationUnsupportedError =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.spaceObject

Returns the value of attribute space.



7
8
9
# File 'lib/rspec/mocks.rb', line 7

def space
  @space
end

Class Method Details

.allow_message(subject, message, opts = {}, &block) ⇒ 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

  • block

    an optional implementation for the allowance



59
60
61
62
63
64
65
# File 'lib/rspec/mocks.rb', line 59

def allow_message(subject, message, opts={}, &block)
  orig_caller = opts.fetch(:expected_from) {
    CallerFilter.first_non_rspec_line
  }
  ::RSpec::Mocks.proxy_for(subject).
    add_stub(orig_caller, message.to_sym, opts, &block)
end

.any_instance_recorder_for(klass) ⇒ Object



43
44
45
# File 'lib/rspec/mocks.rb', line 43

def any_instance_recorder_for(klass)
  space.any_instance_recorder_for(klass)
end

.configurationObject



76
77
78
# File 'lib/rspec/mocks/configuration.rb', line 76

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

.const_missing(name) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/rspec/mocks.rb', line 117

def self.const_missing(name)
  if const = DEPRECATED_CONSTANTS[name]
    RSpec.deprecate("RSpec::Mocks::#{name}", :replacement => const.name)
    const
  else
    super
  end
end

.expect_message(subject, message, opts = {}, &block) ⇒ 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

  • block

    an optional implementation for the expectation



78
79
80
81
82
83
84
# File 'lib/rspec/mocks.rb', line 78

def expect_message(subject, message, opts={}, &block)
  orig_caller = opts.fetch(:expected_from) {
    CallerFilter.first_non_rspec_line
  }
  ::RSpec::Mocks.proxy_for(subject).
    add_message_expectation(orig_caller, message.to_sym, opts, &block)
end

.method_handle_for(object, method_name) ⇒ Object

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)


97
98
99
100
101
102
103
# File 'lib/rspec/mocks.rb', line 97

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

.proxies_of(klass) ⇒ Object



39
40
41
# File 'lib/rspec/mocks.rb', line 39

def proxies_of(klass)
  space.proxies_of(klass)
end

.proxy_for(object) ⇒ Object



35
36
37
# File 'lib/rspec/mocks.rb', line 35

def proxy_for(object)
  space.proxy_for(object)
end

.setup(host = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec/mocks.rb', line 9

def setup(host=nil)
  host_is_from_rspec_core = defined?(::RSpec::Core::ExampleGroup) && host.is_a?(::RSpec::Core::ExampleGroup)
  if host
    unless host_is_from_rspec_core
      RSpec.deprecate(
        "The host argument to `RSpec::Mocks.setup`",
        :replacement => "`include RSpec::Mocks::ExampleMethods` in #{host.inspect}"
      )
    end

    (class << host; self; end).class_eval do
      include RSpec::Mocks::ExampleMethods
    end
  end
  space.outside_example = false
end

.teardownObject



30
31
32
33
# File 'lib/rspec/mocks.rb', line 30

def teardown
  space.reset_all
  space.outside_example = true
end

.verifyObject



26
27
28
# File 'lib/rspec/mocks.rb', line 26

def verify
  space.verify_all
end