# rspec/rspec-mocks

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/rspec-rspec-mocks).**

1,148 stars · 352 forks · Ruby · MIT · archived

## Links

- GitHub: https://github.com/rspec/rspec-mocks
- Homepage: https://rspec.info
- awesome-repositories: https://awesome-repositories.com/repository/rspec-rspec-mocks.md

## Topics

`rspec` `ruby`

## Description

This project is a Ruby unit testing tool designed for dependency isolation and behavioral contract verification. It functions as a comprehensive mocking framework that allows developers to replace real objects with controlled placeholders, ensuring that code interacts with its dependencies as expected during automated test execution.

The framework distinguishes itself through its ability to perform partial double verification, which checks that stubbed methods exist on the underlying object to prevent tests from passing against incorrect interfaces. It also provides sophisticated constant management, enabling the temporary overriding, hiding, or stubbing of global constants to maintain environment consistency. Furthermore, the tool supports real object instrumentation and instance-level method injection, allowing for the modification of specific object behavior without affecting global class definitions.

Beyond these core capabilities, the library offers extensive utilities for interaction verification, including support for call ordering enforcement, interaction frequency control, and the implementation of test spies. It facilitates legacy codebase refactoring by allowing developers to apply expectations to existing classes and instances, and it includes mechanisms for creating null object doubles to simplify dependency management.

The framework provides command-line utilities for running test suites and generating reproducible scripts to assist in debugging. It also supports environment customization through supplemental configuration files, allowing for the injection of dependencies without altering the primary project architecture.

## Tags

### Testing & Quality Assurance

- [Test Environment Mocks](https://awesome-repositories.com/f/testing-quality-assurance/application-unit-testing/test-environment-mocks.md) — Replaces real objects with test doubles to simulate dependencies and verify interactions without executing actual production logic. ([source](https://rspec.info/))
- [Ruby Testing Frameworks](https://awesome-repositories.com/f/testing-quality-assurance/automated-test-suites/ruby-testing-frameworks.md) — Provides a comprehensive framework for building and running test suites in Ruby applications.
- [Interaction Expectations](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations.md) — Provides the primary mechanism for asserting that specific methods are called with expected arguments and frequencies. ([source](https://github.com/rspec/rspec-mocks#readme))
- [Behavioral Verification Tools](https://awesome-repositories.com/f/testing-quality-assurance/behavioral-verification-tools.md) — Asserts that specific methods are called with expected arguments, frequencies, and sequences in Ruby applications.
- [Dependency-Mocked Unit Testers](https://awesome-repositories.com/f/testing-quality-assurance/general-testing-utilities/test-isolation/middleware-unit-testing/dependency-mocked-unit-testers.md) — Replaces real objects with test doubles to simulate external dependencies and verify interactions without executing actual production logic.
- [Object Interaction Verification](https://awesome-repositories.com/f/testing-quality-assurance/object-interaction-verification.md) — Validates behavioral contracts by asserting that objects receive expected messages with correct arguments during test execution. ([source](https://github.com/rspec/rspec-mocks/tree/main/features))
- [Method Stubbing Utilities](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/testing-frameworks/test-frameworks/assertions-and-validation/test-case-definitions/method-stubbing-utilities.md) — Forces methods to return specific values or perform defined actions to manage external dependencies during test execution. ([source](https://github.com/rspec/rspec-mocks/blob/main/Changelog.md))
- [Unit Testing Utilities](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/testing-frameworks/test-frameworks/test-levels-and-types/unit-testing-utilities.md) — Isolates code dependencies by replacing real objects with controlled placeholders during automated test execution.
- [Test Double Libraries](https://awesome-repositories.com/f/testing-quality-assurance/test-double-libraries.md) — Creates test doubles, stubs method calls, and verifies message interactions within Ruby unit testing suites.
- [Expectation Registries](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/expectation-registries.md) — Maintains a central collection of expected method signatures and call counts to validate behavioral contracts after test completion.
- [Mock Call Order Verifications](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/ordered/mock-call-order-verifications.md) — Enforces specific sequences of method calls on mock objects to validate complex interaction flows during test execution. ([source](https://github.com/rspec/rspec-mocks/blob/main/README.md))
- [Mock Call Count Verification](https://awesome-repositories.com/f/testing-quality-assurance/api-expectation-frameworks/interaction-expectations/ordered/mock-call-order-verifications/mock-call-count-verification.md) — Allows precise control over the expected number of method invocations, supporting exact, minimum, or maximum call counts. ([source](https://github.com/rspec/rspec-mocks/blob/main/README.md))
- [Interface Introspection](https://awesome-repositories.com/f/testing-quality-assurance/method-invocation-verifications/method-signature-assertions/interface-introspection.md) — Queries the underlying object hierarchy at runtime to verify that stubbed methods exist and match the expected interface.
- [Object Instrumentation](https://awesome-repositories.com/f/testing-quality-assurance/object-instrumentation.md) — Overrides methods on existing objects to inject test-specific behavior and automatically restores the original implementation after tests. ([source](https://github.com/rspec/rspec-mocks#readme))
- [Class-Wide Stubbing](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/dependency-mocking/mock-instance-detectors/class-wide-stubbing.md) — Applies method stubs or message expectations to every instance of a specific class to handle dependencies in complex codebases. ([source](https://github.com/rspec/rspec-mocks#readme))
- [Partial Mocks](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/mocking-frameworks/partial-mocks.md) — Verifies that methods stubbed on real objects actually exist, preventing tests from passing against incorrect interfaces. ([source](https://github.com/rspec/rspec-mocks/blob/main/Changelog.md))
- [Reproducible Script Generators](https://awesome-repositories.com/f/testing-quality-assurance/software-testing/testing-frameworks/test-frameworks/assertions-and-validation/test-case-definitions/bug-validation-tests/inline-test-case-reproducers/reproducible-script-generators.md) — Generates self-contained scripts that isolate specific software behaviors or bugs to simplify debugging and reporting. ([source](https://github.com/rspec/rspec-mocks/blob/main/REPORT_TEMPLATE.md))
- [Contract Verifiers](https://awesome-repositories.com/f/testing-quality-assurance/test-double-libraries/contract-verifiers.md) — Validates that stubbed methods exist on the underlying object and enforces correct argument counts to ensure test doubles remain synchronized. ([source](https://github.com/rspec/rspec-mocks/blob/main/features/verifying_doubles))
- [Null Object Doubles](https://awesome-repositories.com/f/testing-quality-assurance/test-double-libraries/null-object-doubles.md) — Provides null object doubles that return themselves for any method call, acting as flexible placeholders that eliminate the need for explicit stubbing. ([source](https://github.com/rspec/rspec-mocks/blob/main/Changelog.md))
- [Interaction Spies](https://awesome-repositories.com/f/testing-quality-assurance/test-spying-utilities/interaction-spies.md) — Captures method invocation history on objects to enable post-execution verification of behavior using the test spy pattern. ([source](https://github.com/rspec/rspec-mocks#readme))
- [Interaction Sequence Orderers](https://awesome-repositories.com/f/testing-quality-assurance/testing-infrastructure-management/test-execution-management/execution-ordering/interaction-sequence-orderers.md) — Enforces the required sequence of method calls across mock objects to validate complex interaction flows.

### Development Tools & Productivity

- [Automated Test Execution](https://awesome-repositories.com/f/development-tools-productivity/debugging-profiling-testing/test-execution-management/automated-test-execution.md) — Provides command-line interfaces to run automated test suites and verify code behavior. ([source](https://github.com/rspec/rspec-mocks/blob/main/DEV-README.md))
- [Legacy Code Testing](https://awesome-repositories.com/f/development-tools-productivity/code-refactoring-tools/legacy-code-testing.md) — Facilitates refactoring by applying method stubs and expectations to existing classes and instances to safely test tightly coupled code.
- [Mock Constant Overriders](https://awesome-repositories.com/f/development-tools-productivity/predefined-constants/mock-constant-overriders.md) — Replaces a constant with a specified value during test execution and automatically restores the original value once the test completes. ([source](https://github.com/rspec/rspec-mocks/blob/main/features/mutating_constants/README.md))
- [Constant Hiding Utilities](https://awesome-repositories.com/f/development-tools-productivity/predefined-constants/mock-constant-overriders/constant-hiding-utilities.md) — Removes constants from the environment during test execution and automatically restores them to their original state. ([source](https://github.com/rspec/rspec-mocks/blob/main/features/mutating_constants/README.md))

### Programming Languages & Runtimes

- [Method Stubbing](https://awesome-repositories.com/f/programming-languages-runtimes/block-value-returns/method-return-values/return-value-interceptors/method-stubbing.md) — Replaces real method behavior with pre-determined return values on test doubles and existing objects to control dependencies. ([source](https://github.com/rspec/rspec-mocks#readme))
- [Instance-Level Injections](https://awesome-repositories.com/f/programming-languages-runtimes/class-method-definitions/singleton-classes/instance-level-injections.md) — Modifies the singleton class of specific object instances to override behavior without affecting the global class definition.

### Software Engineering & Architecture

- [Behavioral Contract Validation](https://awesome-repositories.com/f/software-engineering-architecture/api-contract-definitions/api-contract-linting/type-based-contract-validation/behavioral-contract-validation.md) — Asserts that objects receive expected messages with specific arguments and frequencies to ensure code interacts correctly with its dependencies.
- [Constant Rebinding](https://awesome-repositories.com/f/software-engineering-architecture/constant-value-assignments/constant-rebinding.md) — Provides mechanisms to temporarily modify global constants during test execution to maintain environment consistency.
- [Method Interception Proxies](https://awesome-repositories.com/f/software-engineering-architecture/method-interception-proxies.md) — Wraps existing object methods with dynamic proxies to intercept calls and inject custom behavior during test execution.
- [Argument Matchers](https://awesome-repositories.com/f/software-engineering-architecture/static-type-checkers/validation-logic/cli-argument-validators/test-argument-validators/argument-matchers.md) — Constrains expectations to specific argument values or patterns using matchers to ensure methods are invoked with the correct data. ([source](https://github.com/rspec/rspec-mocks/blob/main/README.md))
