A mocking and stubbing library for Ruby
Mocha is intended to be used in unit tests for the Mock Object or Test Stub types of Test Double, not the Fake Object or Test Spy types. Although it would be possible to extend Mocha to allow the implementation of fakes and spies, we have chosen to keep it focused on mocks and stubs.
Install the latest version of the gem with the following command...
$ gem install mochaNote: If you are intending to use Mocha with Test::Unit or Minitest, you should only setup Mocha after loading the relevant test library...
require 'rubygems'
gem 'mocha'
require 'test/unit'
require 'mocha/test_unit'require 'rubygems'
gem 'mocha'
require 'minitest/autorun'
require 'mocha/minitest'If you're using Bundler, include Mocha in the Gemfile and then setup Mocha later once you know the test library has been loaded...
# Gemfile
gem 'mocha'
# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
require 'test/unit'
require 'mocha/test_unit'# Gemfile
gem 'mocha'
# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
require 'minitest/autorun'
require 'mocha/minitest'RSpec includes a mocha adapter. Just tell RSpec you want to mock with :mocha:
# Gemfile in Rails app
gem 'mocha'
# Within `spec/spec_helper.rb`
RSpec.configure do |config|
config.mock_with :mocha
endNote: There is no need to use a require statement to setup Mocha; RSpec does this itself.
# In e.g. features/support/mocha.rb
require 'mocha/api'
World(Mocha::API)
Around do |scenario, block|
begin
mocha_setup
block.call
mocha_verify
ensure
mocha_teardown
end
endIf you're loading Mocha using Bundler within a Rails application, you should setup Mocha manually e.g. at the bottom of your test_helper.rb.
Note that since Rails v4 (at least), ActiveSupport::TestCase has inherited from Minitest::Test or its earlier equivalents. Thus unless you are explicitly using Test::Unit, you are likely to be using Minitest.
# Gemfile in Rails app
gem 'mocha'
# At bottom of test_helper.rb (or at least after `require 'rails/test_help'`)
require 'mocha/minitest'Follow the instructions for the relevant test framework in the Bundler section, but ensure that the relevant Mocha file (mocha/minitest, mocha/test_unit, or mocha/api) is required after the test framework has been loaded, e.g. at the bottom of test_helper.rb or spec_helper.rb, or at least after rails/test_help has been required.
extend the class doesn't work in Ruby v1.8. See test/acceptance/stub_method_defined_on_module_and_aliased_test.rb for an example of this behaviour.require 'test/unit'
require 'mocha/test_unit'
class MiscExampleTest date }.length
end
def unshipped_value
find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
end
end
end
require 'test/unit'
require 'mocha/test_unit'
class OrderTest ' https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials$ bundle exec rake release
[runs tests]
mocha 1.2.0 built to pkg/mocha-1.2.0.gem.
Tagged v1.2.0.
Pushed git commits and tags.
Pushed mocha 1.2.0 to rubygems.org.Mocha was initially harvested from projects at Reevoo. It's syntax is heavily based on that of jMock.
© Copyright James Mead 2006
You may use, copy and redistribute this library under the same terms as Ruby itself or under the MIT license.
No open issues yet, or sync has not completed.