Module Mocha::MockMethods
In: lib/mocha/mock_methods.rb

Methods added to mock objects. These methods all return an expectation which can be further modified by methods on Mocha::Expectation.

Methods

Public Instance methods

__expects__(method_names, backtrace = nil)

Alias for expects

__stubs__(method_names, backtrace = nil)

Alias for stubs

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 Mocha::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)

[Source]

    # File lib/mocha/mock_methods.rb, line 41
41:     def expects(method_names, backtrace = nil)
42:       method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
43:       method_names.each do |method_name, return_value|
44:         expectations << Expectation.new(self, method_name, backtrace).returns(return_value)
45:         self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
46:       end
47:       expectations.last
48:     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 Mocha::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)

[Source]

    # File lib/mocha/mock_methods.rb, line 71
71:     def stubs(method_names, backtrace = nil)
72:       method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
73:       method_names.each do |method_name, return_value|
74:         expectations << Stub.new(self, method_name, backtrace).returns(return_value)
75:         self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
76:       end
77:       expectations.last
78:     end

[Validate]