Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. Mocha

Mocha

NOASSERTIONRuby

A Ruby library for mocking and stubbing objects in unit tests with a unified syntax for Minitest, Test::Unit, and RSpec.

Visit WebsiteGitHubGitHub
1.3k stars159 forks0 contributors

What is Mocha?

Mocha is a Ruby library for creating mock objects and stubs in unit tests. It allows developers to replace real dependencies with controlled test doubles, isolating the code under test and verifying interactions. The library provides a unified syntax that works with popular Ruby test frameworks like Minitest, Test::Unit, and RSpec.

Target Audience

Ruby developers writing unit tests who need to mock external dependencies or stub methods for isolated testing. It's particularly useful for teams using Minitest, Test::Unit, or RSpec.

Value Proposition

Developers choose Mocha for its clean, readable syntax and framework-agnostic approach to mocking. Unlike more complex alternatives, it focuses specifically on mocking and stubbing with minimal setup, making tests easier to write and maintain.

Overview

A mocking and stubbing library for Ruby

Use Cases

Best For

  • Mocking external API calls in Ruby unit tests
  • Stubbing database queries to test business logic in isolation
  • Testing service objects with complex dependencies
  • Verifying method call expectations in test-driven development
  • Partial mocking of specific methods on ActiveRecord models
  • Writing unit tests for Ruby gems with external dependencies

Not Ideal For

  • Multi-threaded test suites where thread safety is essential
  • Projects requiring spy functionality for indirect behavior verification without full mocks
  • Teams seeking an all-in-one test double library with fakes included for integrated testing

Pros & Cons

Pros

Unified Mocking Syntax

Offers a simple, consistent API for both full and partial mocking, as demonstrated in the quick start examples with expects and stubs methods.

Broad Framework Support

Integrates seamlessly with Minitest, Test::Unit, and RSpec via built-in adapters, with detailed installation instructions for each in the README.

Partial Mocking Flexibility

Enables stubbing specific methods on real objects without replacing entire instances, shown in examples like stubbing total_weight on an Order object.

Configurable Validation

Provides warnings for common issues like stubbing non-existent methods, as mentioned in the configuration documentation to improve test robustness.

Cons

No Thread Safety

Explicitly stated in the README that Mocha does not attempt to be thread-safe, making it unreliable for testing multi-threaded code or running tests concurrently.

Limited Test Double Types

Focuses only on mocks and stubs, deliberately avoiding fakes and spies, which may require additional libraries for comprehensive testing needs.

Setup Order Sensitivity

Requires careful ordering when loading with test frameworks (e.g., after Minitest or RSpec), as improper setup can cause integration issues, noted in installation steps.

Frequently Asked Questions

Quick Stats

Stars1,285
Forks159
Contributors0
Open Issues115
Last commit11 days ago
CreatedSince 2008

Tags

#test-unit#unit-testing#stub#test-doubles#minitest#mocking#testing#mock#rspec#stubbing#ruby

Built With

R
Ruby

Links & Resources

Website

Included in

Ruby14.1k
Auto-fetched 17 hours ago

Related Projects

fakerfaker

A library for generating fake data such as names, addresses, and phone numbers.

Stars11,914
Forks3,211
Last commit2 days ago
CapybaraCapybara

Acceptance test framework for web applications

Stars10,166
Forks1,473
Last commit11 days ago
factory_botfactory_bot

A library for setting up Ruby objects as test data.

Stars8,182
Forks2,560
Last commit9 days ago
VCRVCR

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

Stars6,076
Forks510
Last commit1 month ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub