Class: Spec::Mocks::Mock
- Includes:
- Methods
- Defined in:
- lib/gems/rspec-1.1.12/lib/spec/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 By making the other object run the comparison, we’re sure the call gets delegated to the proxy target This is an unfortunate side effect from ActiveRecord, but this should be safe unless the RHS redefines == in a nonsensical manner.
-
#initialize(name, stubs_and_options = {}) ⇒ Mock
constructor
Creates a new mock with a
name
(that will be used in error messages only) == Options: *:null_object
- if true, the mock object acts as a forgiving null object allowing any message to be sent to it. - #inspect ⇒ Object
- #method_missing(sym, *args, &block) ⇒ Object
- #to_s ⇒ Object
Methods included from Methods
#as_null_object, #null_object?, #received_message?, #rspec_reset, #rspec_verify, #should_not_receive, #should_receive, #stub!
Constructor Details
#initialize(name, stubs_and_options = {}) ⇒ Mock
Creates a new mock with a name
(that will be used in error messages only) == Options:
-
:null_object
- if true, the mock object acts as a forgiving null object allowing any message to be sent to it.
10 11 12 13 14 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/mock.rb', line 10 def initialize(name, ={}) @name = name @options = () assign_stubs() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/mock.rb', line 25 def method_missing(sym, *args, &block) __mock_proxy.instance_eval {@messages_received << [sym, args, block]} begin return self if __mock_proxy.null_object? super(sym, *args, &block) 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 This is an unfortunate side effect from ActiveRecord, but this should be safe unless the RHS redefines == in a nonsensical manner
21 22 23 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/mock.rb', line 21 def ==(other) other == __mock_proxy end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/mock.rb', line 35 def inspect "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>" end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/gems/rspec-1.1.12/lib/spec/mocks/mock.rb', line 39 def to_s inspect.gsub('<','[').gsub('>',']') end |