Included Modules

RSpec::Mocks::TestDouble

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.

Public Class Methods

extend_onto(object, name=nil, stubs_and_options={}) click to toggle source

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

@example

  module = Module.new
  RSpec::Mocks::TestDouble.extend_onto(module, "MyMixin", :foo => "bar")
  module.foo  #=> "bar"
    # File lib/rspec/mocks/test_double.rb, line 17
17:       def self.extend_onto(object, name=nil, stubs_and_options={})
18:         object.extend self
19:         object.send(:__initialize_as_test_double, name, stubs_and_options)
20:       end
new(name=nil, stubs_and_options={}) click to toggle source

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

    # File lib/rspec/mocks/test_double.rb, line 24
24:       def initialize(name=nil, stubs_and_options={})
25:         __initialize_as_test_double(name, stubs_and_options)
26:       end

Public Instance Methods

==(other) click to toggle source

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.

    # File lib/rspec/mocks/test_double.rb, line 32
32:       def ==(other)
33:         other == __mock_proxy
34:       end
inspect() click to toggle source

@private

    # File lib/rspec/mocks/test_double.rb, line 37
37:       def inspect
38:         "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
39:       end
respond_to?(message, incl_private=false) click to toggle source

@private

    # File lib/rspec/mocks/test_double.rb, line 49
49:       def respond_to?(message, incl_private=false)
50:         __mock_proxy.null_object? && message != :to_ary ? true : super
51:       end
to_s() click to toggle source

@private

    # File lib/rspec/mocks/test_double.rb, line 42
42:       def to_s
43:         inspect.gsub('<','[').gsub('>',']')
44:       end
Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

Private Instance Methods

__initialize_as_test_double(name=nil, stubs_and_options={}) click to toggle source
    # File lib/rspec/mocks/test_double.rb, line 55
55:       def __initialize_as_test_double(name=nil, stubs_and_options={})
56:         if name.is_a?(Hash) && stubs_and_options.empty?
57:           stubs_and_options = name
58:           @name = nil
59:         else
60:           @name = name
61:         end
62:         @options = extract_options(stubs_and_options)
63:         assign_stubs(stubs_and_options)
64:       end
assign_stubs(stubs) click to toggle source
    # File lib/rspec/mocks/test_double.rb, line 94
94:       def assign_stubs(stubs)
95:         stubs.each_pair do |message, response|
96:           stub(message).and_return(response)
97:         end
98:       end
extract_option(source, target, key, default=nil) click to toggle source
    # File lib/rspec/mocks/test_double.rb, line 86
86:       def extract_option(source, target, key, default=nil)
87:         if source[key]
88:           target[key] = source.delete(key)
89:         elsif default
90:           target[key] = default
91:         end
92:       end
extract_options(stubs_and_options) click to toggle source
    # File lib/rspec/mocks/test_double.rb, line 76
76:       def extract_options(stubs_and_options)
77:         if stubs_and_options[:null_object]
78:           @null_object = stubs_and_options.delete(:null_object)
79:           RSpec.deprecate(%["double('name', :null_object => true)"], %["double('name').as_null_object"])
80:         end
81:         options = {}
82:         extract_option(stubs_and_options, options, :__declared_as, 'Mock')
83:         options
84:       end
method_missing(message, *args, &block) click to toggle source
    # File lib/rspec/mocks/test_double.rb, line 66
66:       def method_missing(message, *args, &block)
67:           raise NoMethodError if message == :to_ary
68:         __mock_proxy.record_message_received(message, *args, &block)
69:         begin
70:           __mock_proxy.null_object? ? self : super
71:         rescue NameError
72:           __mock_proxy.raise_unexpected_message_error(message, *args)
73:         end
74:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.