Module: RSpec::Mocks::TestDouble
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
-
.extend_onto(object, name = nil, stubs_and_options = {}) ⇒ Object
Extends the TestDouble module onto the given object and initializes it as a test double.
Instance Method Summary collapse
-
#==(other) ⇒ Object
This allows for comparing the mock to other objects that proxy such as ActiveRecords belongs_to proxy objects.
-
#initialize(name = nil, stubs_and_options = {}) ⇒ Object
Creates a new test double with a
name(that will be used in error messages only).
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)
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rspec/mocks/test_double.rb', line 66 def method_missing(, *args, &block) raise NoMethodError if == :to_ary return 0 if == :to_int && __mock_proxy.null_object? __mock_proxy.(, *args, &block) begin __mock_proxy.null_object? ? self : super rescue NameError __mock_proxy.(, *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.
17 18 19 20 |
# File 'lib/rspec/mocks/test_double.rb', line 17 def self.extend_onto(object, name=nil, ={}) object.extend self object.send(:__initialize_as_test_double, name, ) 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, ={}) __initialize_as_test_double(name, ) end |