Module: RSpec::Mocks::TestDouble

Includes:
Methods
Included in:
Mock
Defined in:
lib/rspec/mocks/test_double.rb

Overview

Implements the methods needed for a pure test double. RSpec::Mocks::Mock includes this module, and it is provided for cases where you want a pure test double without subclassing RSpec::Mocks::Mock.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#as_null_object, #null_object?, #should_not_receive, #should_receive, #stub, #stub_chain, #unstub

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args, &block) ⇒ Object (private)

Raises:

  • (NoMethodError)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec/mocks/test_double.rb', line 66

def method_missing(message, *args, &block)
  raise NoMethodError if message == :to_ary
  return 0 if message == :to_int && __mock_proxy.null_object?
  __mock_proxy.record_message_received(message, *args, &block)

  begin
    __mock_proxy.null_object? ? self : super
  rescue NameError
    __mock_proxy.raise_unexpected_message_error(message, *args)
  end
end

Class Method Details

.extend_onto(object, name = nil, stubs_and_options = {}) ⇒ Object

Extends the TestDouble module onto the given object and initializes it as a test double.

Examples:


module = Module.new
RSpec::Mocks::TestDouble.extend_onto(module, "MyMixin", :foo => "bar")
module.foo  #=> "bar"


17
18
19
20
# File 'lib/rspec/mocks/test_double.rb', line 17

def self.extend_onto(object, name=nil, stubs_and_options={})
  object.extend self
  object.send(:__initialize_as_test_double, name, stubs_and_options)
end

Instance Method Details

#==(other) ⇒ Object

This allows for comparing the mock to other objects that proxy such as ActiveRecords belongs_to proxy objects. By making the other object run the comparison, we're sure the call gets delegated to the proxy target.



32
33
34
# File 'lib/rspec/mocks/test_double.rb', line 32

def ==(other)
  other == __mock_proxy
end

#initialize(name = nil, stubs_and_options = {}) ⇒ Object

Creates a new test double with a name (that will be used in error messages only)



24
25
26
# File 'lib/rspec/mocks/test_double.rb', line 24

def initialize(name=nil, stubs_and_options={})
  __initialize_as_test_double(name, stubs_and_options)
end