Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mocha

> 测试质量
Open source

A mocking and stubbing library for Ruby

1.3K stars0 likes2 views
WebsiteGitHub

About

A mocking and stubbing library for Ruby

Mocha

Description

  • A Ruby library for mocking and stubbing - but deliberately not (yet) faking or spying.
  • A unified, simple and readable syntax for both full & partial mocking.
  • Built-in support for Minitest and Test::Unit.
  • Supported by many other test frameworks.

Intended Usage

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.

Installation

Gem

Install the latest version of the gem with the following command...

$ gem install mocha

Note: If you are intending to use Mocha with Test::Unit or Minitest, you should only setup Mocha after loading the relevant test library...

Test::Unit
ruby
require 'rubygems'
gem 'mocha'
require 'test/unit'
require 'mocha/test_unit'
Minitest
ruby
require 'rubygems'
gem 'mocha'
require 'minitest/autorun'
require 'mocha/minitest'

Bundler

If you're using Bundler, include Mocha in the Gemfile and then setup Mocha later once you know the test library has been loaded...

Test::Unit
ruby
# Gemfile
gem 'mocha'

# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
require 'test/unit'
require 'mocha/test_unit'
Minitest
ruby
# Gemfile
gem 'mocha'

# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
require 'minitest/autorun'
require 'mocha/minitest'
RSpec

RSpec includes a mocha adapter. Just tell RSpec you want to mock with :mocha:

ruby
# Gemfile in Rails app
gem 'mocha'

# Within `spec/spec_helper.rb`
RSpec.configure do |config|
  config.mock_with :mocha
end

Note: There is no need to use a require statement to setup Mocha; RSpec does this itself.

Cucumber
ruby
# 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
end

Rails

If 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.

Minitest

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.

ruby
# Gemfile in Rails app
gem 'mocha'

# At bottom of test_helper.rb (or at least after `require 'rails/test_help'`)
require 'mocha/minitest'
Other Test Framework

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.

Known Issues

  • Prior to v1.15.0 (when support for Ruby v1.8 was dropped), stubbing an aliased class method where the original method is defined in a module that's used to 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.

Usage

Quick Start

ruby
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
  • Release gem to Rubygems:
bash
$ 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.

History

Mocha was initially harvested from projects at Reevoo. It's syntax is heavily based on that of jMock.

License

© Copyright James Mead 2006

You may use, copy and redistribute this library under the same terms as Ruby itself or under the MIT license.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyminitestmockrubystub

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.