Module: RSpec::Mocks::TestDouble
- Included in:
- Double
- 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)
-
+ (Object) extend_onto(object, name = nil, stubs_and_options = {})
Extends the TestDouble module onto the given object and initializes it as a test double.
Instance Method Summary (collapse)
-
- (Object) ==(other)
This allows for comparing the mock to other objects that proxy such as ActiveRecords belongs_to proxy objects.
- - (Object) __warn_if_used_further!
-
- (Object) as_null_object
Tells the object to respond to all messages.
- - (Object) freeze
-
- (TestDouble) initialize(name = nil, stubs_and_options = {})
Creates a new test double with a
name
(that will be used in error messages only). -
- (Boolean) null_object?
Returns true if this object has received
as_null_object
.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(message, *args) (private)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rspec/mocks/test_double.rb', line 100 def method_missing(, *args, &block) if __mock_proxy.null_object? case when :to_int then return 0 when :to_a, :to_ary then return nil end end __mock_proxy.(, *args, &block) begin __mock_proxy.null_object? ? self : super rescue NameError # Required wrapping doubles in an Array on Ruby 1.9.2 raise NoMethodError if [:to_a, :to_ary].include? __mock_proxy.(, *args) end end |
Class Method Details
+ (Object) extend_onto(object, name = nil, stubs_and_options = {})
Extends the TestDouble module onto the given object and initializes it as a test double.
15 16 17 18 19 |
# File 'lib/rspec/mocks/test_double.rb', line 15 def self.extend_onto(object, name=nil, ={}) RSpec.deprecate("`RSpec::Mocks::TestDouble.extend_onto(...)`") object.extend self object.send(:__initialize_as_test_double, name, ) end |
Instance Method Details
- (Object) ==(other)
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.
46 47 48 |
# File 'lib/rspec/mocks/test_double.rb', line 46 def ==(other) other == __mock_proxy end |
- (Object) __warn_if_used_further!
80 81 82 |
# File 'lib/rspec/mocks/test_double.rb', line 80 def __warn_if_used_further! @__unfrozen_attributes[:expired] = true end |
- (Object) as_null_object
Tells the object to respond to all messages. If specific stub values are declared, they'll work as expected. If not, the receiver is returned.
30 31 32 33 34 |
# File 'lib/rspec/mocks/test_double.rb', line 30 def as_null_object __mock_proxy.as_null_object @__null_object = true self end |
- (Object) freeze
67 68 69 70 |
# File 'lib/rspec/mocks/test_double.rb', line 67 def freeze RSpec.deprecate 'Freezing a test double' super end |
- (TestDouble) initialize(name = nil, stubs_and_options = {})
Creates a new test double with a name
(that will be used in error
messages only)
23 24 25 |
# File 'lib/rspec/mocks/test_double.rb', line 23 def initialize(name=nil, ={}) __initialize_as_test_double(name, ) end |
- (Boolean) null_object?
Returns true if this object has received as_null_object
37 38 39 40 |
# File 'lib/rspec/mocks/test_double.rb', line 37 def null_object? __warn_of_expired_use_if_expired @__null_object end |