Build Status

A fast, tiny (82 lines) hybrid mocking library that supports classical and partial mocking. Partial mocking mixes classical mocking with real objects. There's no monkey patching Object or copying. Mock objects are isolated leaving real objects completely untainted. Plays nicely with MiniTest and RSpec. The interface is 100% compatible with MiniTest::Mock so there is nothing new to learn. SimpleMock's one and only dependancy is Ruby 1.9.2 or greater.


Add this to your project's Gemfile and run $ bundle.

gem 'simple_mock', :group => :test

SimpleMock is isolated so there is no need to set require to false.


Classical Mocking

A new SimpleMock object behaves identically to MiniTest::Mock.

mock_model =
mock_model.expect :valid?, true

mock_model.valid? # => true

mock_model.verify # => true

Partial Mocking

Pass an object to mix expectations with the real object's original behaviour.

class Post < ActiveRecord::Base
  validates :title, :presence => true

real_model =
mock_model = real_model
mock_model.class # => Post

mock_model.expect :valid?, true

mock_model.valid? # => true
mock_model.create # => true

mock_model.verify # => true

This is done with delegation, avoiding monkey patching and copying. The real object is completely untainted.

mock_model.valid  # => true
real_model.valid? # => false

real_model.object_id == mock_model.__getobj__.object_id # => true
real_model.object_id != mock_model.object_id            # => true

More documentation is available at


SimpleMock is fast. In this benchmark we create an array, set an expectation and call that method 10,000 times.

             user       system     total      real
mocha:       0.000000   0.000000   0.000000   (0.000279)
simple_mock: 0.000000   0.000000   0.000000   (0.000057)


Like MiniTest::Mock, #expect and #verify are reserved methods. Expectations should not be defined on real objects which implement these methods. As an alternative, consider creating an anonymous class which inherits from SimpleDelegator.

mock_class = SimpleDelegator do
  def verify *args
mock_instance =
mock_instance.verify # => true

SimpleMock does something similar to this under the hood.

Copyright © 2012 Tate Johnson. SimpleMock is released under the MIT license. See LICENSE for details.