Class: RSpec::Mocks::Mock
- Includes:
- Methods
- Defined in:
- lib/rspec/mocks/mock.rb
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 = {}) ⇒ Mock
constructor
Creates a new test double with a
name
(that will be used in error messages only). - #inspect ⇒ Object
- #respond_to?(sym, incl_private = false) ⇒ Boolean
- #to_s ⇒ Object (also: #to_str)
Methods included from Methods
#as_null_object, #null_object?, #received_message?, #rspec_reset, #rspec_verify, #should_not_receive, #should_receive, #stub, #stub_chain, #unstub
Constructor Details
#initialize(name = nil, stubs_and_options = {}) ⇒ Mock
Creates a new test double with a name
(that will be used in error messages only)
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rspec/mocks/mock.rb', line 8 def initialize(name=nil, ={}) if name.is_a?(Hash) && .empty? = name @name = nil else @name = name end @options = () assign_stubs() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object (private)
43 44 45 46 47 48 49 50 51 |
# File 'lib/rspec/mocks/mock.rb', line 43 def method_missing(sym, *args, &block) __mock_proxy.(sym, *args, &block) begin return self if __mock_proxy.null_object? super rescue NameError __mock_proxy.(sym, *args) end 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.
23 24 25 |
# File 'lib/rspec/mocks/mock.rb', line 23 def ==(other) other == __mock_proxy end |
#inspect ⇒ Object
27 28 29 |
# File 'lib/rspec/mocks/mock.rb', line 27 def inspect "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>" end |
#respond_to?(sym, incl_private = false) ⇒ Boolean
37 38 39 |
# File 'lib/rspec/mocks/mock.rb', line 37 def respond_to?(sym, incl_private=false) __mock_proxy.null_object? ? true : super end |
#to_s ⇒ Object Also known as: to_str
31 32 33 |
# File 'lib/rspec/mocks/mock.rb', line 31 def to_s inspect.gsub('<','[').gsub('>',']') end |