Class Mocha::Mock
In: lib/mocha/mock.rb
Parent: Object

Traditional mock object.

Methods return an Expectation which can be further modified by methods on Expectation.

Methods

Public Instance methods

Adds an expectation that a method identified by method_name symbol must be called exactly once with any parameters. Returns the new expectation which can be further modified by methods on Expectation.

  object = mock()
  object.expects(:method1)
  object.method1
  # no error raised

  object = mock()
  object.expects(:method1)
  # error raised, because method1 not called exactly once

If method_names is a Hash, an expectation will be set up for each entry using the key as method_name and value as return_value.

  object = mock()
  object.expects(:method1 => :result1, :method2 => :result2)

  # exactly equivalent to

  object = mock()
  object.expects(:method1).returns(:result1)
  object.expects(:method2).returns(:result2)

Aliased by __expects__

[Source]

    # File lib/mocha/mock.rb, line 40
40:     def expects(method_name_or_hash, backtrace = nil)
41:       if method_name_or_hash.is_a?(Hash) then
42:         method_name_or_hash.each do |method_name, return_value|
43:           ensure_method_not_already_defined(method_name)
44:           @expectations.add(Expectation.new(self, method_name, backtrace).returns(return_value))
45:         end
46:       else
47:         ensure_method_not_already_defined(method_name_or_hash)
48:         @expectations.add(Expectation.new(self, method_name_or_hash, backtrace))
49:       end
50:     end

Constrains the mock so that it can only expect or stub methods to which responder responds. The constraint is only applied at method invocation time.

A NoMethodError will be raised if the responder does not respond_to? a method invocation (even if the method has been expected or stubbed).

The mock will delegate its respond_to? method to the responder.

  class Sheep
    def chew(grass); end
    def self.number_of_legs; end
  end

  sheep = mock('sheep')
  sheep.expects(:chew)
  sheep.expects(:foo)
  sheep.respond_to?(:chew) # => true
  sheep.respond_to?(:foo) # => true
  sheep.chew
  sheep.foo
  # no error raised

  sheep = mock('sheep')
  sheep.responds_like(Sheep.new)
  sheep.expects(:chew)
  sheep.expects(:foo)
  sheep.respond_to?(:chew) # => true
  sheep.respond_to?(:foo) # => false
  sheep.chew
  sheep.foo # => raises NoMethodError exception

  sheep_class = mock('sheep_class')
  sheep_class.responds_like(Sheep)
  sheep_class.stubs(:number_of_legs).returns(4)
  sheep_class.expects(:foo)
  sheep_class.respond_to?(:number_of_legs) # => true
  sheep_class.respond_to?(:foo) # => false
  assert_equal 4, sheep_class.number_of_legs
  sheep_class.foo # => raises NoMethodError exception

Aliased by quacks_like

[Source]

     # File lib/mocha/mock.rb, line 125
125:     def responds_like(object)
126:       @responder = object
127:       self
128:     end

Adds an expectation that a method identified by method_name symbol may be called any number of times with any parameters. Returns the new expectation which can be further modified by methods on Expectation.

  object = mock()
  object.stubs(:method1)
  object.method1
  object.method1
  # no error raised

If method_names is a Hash, an expectation will be set up for each entry using the key as method_name and value as return_value.

  object = mock()
  object.stubs(:method1 => :result1, :method2 => :result2)

  # exactly equivalent to

  object = mock()
  object.stubs(:method1).returns(:result1)
  object.stubs(:method2).returns(:result2)

Aliased by __stubs__

[Source]

    # File lib/mocha/mock.rb, line 73
73:     def stubs(method_name_or_hash, backtrace = nil)
74:       if method_name_or_hash.is_a?(Hash) then
75:         method_name_or_hash.each do |method_name, return_value|
76:           ensure_method_not_already_defined(method_name)
77:           @expectations.add(Expectation.new(self, method_name, backtrace).at_least(0).returns(return_value))
78:         end
79:       else
80:         ensure_method_not_already_defined(method_name_or_hash)
81:         @expectations.add(Expectation.new(self, method_name_or_hash, backtrace).at_least(0))
82:       end
83:     end

[Validate]