Module: Mocha::ClassMethods

Defined in:
lib/mocha/class_methods.rb

Overview

Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.

Instance Method Summary (collapse)

Instance Method Details

- (Mock) any_instance

Returns a mock object which will detect calls to any instance of this class.

Examples:

Return false to invocation of Product#save for any instance of Product.

Product.any_instance.stubs(:save).returns(false)
product_1 = Product.new
assert_equal false, product_1.save
product_2 = Product.new
assert_equal false, product_2.save

Returns:

  • (Mock)

    a mock object which will detect calls to any instance of this class.

Raises:

  • (StubbingError)

    if attempting to stub method which is not allowed.



54
55
56
57
58
59
# File 'lib/mocha/class_methods.rb', line 54

def any_instance
  if frozen?
    raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
  end
  @any_instance ||= AnyInstance.new(self)
end