MCPcopy Index your code
hub / github.com/asomers/mockall

github.com/asomers/mockall @v0.15.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.15.0 ↗ · + Follow
929 symbols 2,297 edges 188 files 104 documented · 11%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Mockall

A powerful mock object library for Rust.

Build Status Crates.io Documentation

Overview

Mock objects are a powerful technique for unit testing software. A mock object is an object with the same interface as a real object, but whose responses are all manually controlled by test code. They can be used to test the upper and middle layers of an application without instantiating the lower ones, or to inject edge and error cases that would be difficult or impossible to create when using the full stack.

Statically typed languages are inherently more difficult to mock than dynamically typed languages. Since Rust is a statically typed language, previous attempts at creating a mock object library have had mixed results. Mockall incorporates the best elements of previous designs, resulting in it having a rich feature set with a terse and ergonomic interface. Mockall is written in 100% safe and stable Rust.

Usage

Typically mockall is only used by unit tests. To use it this way, add this to your Cargo.toml:

[dev-dependencies]
mockall = "0.15.0"

Then use it like this:

#[cfg(test)]
use mockall::{automock, mock, predicate::*};
#[cfg_attr(test, automock)]
trait MyTrait {
    fn foo(&self, x: u32) -> u32;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mytest() {
        let mut mock = MockMyTrait::new();
        mock.expect_foo()
            .with(eq(4))
            .times(1)
            .returning(|x| x + 1);
        assert_eq!(5, mock.foo(4));
    }
}

See the API docs for more information.

Minimum Supported Rust Version (MSRV)

Mockall is supported on Rust 1.77.0 and higher. Mockall's MSRV will not be changed in the future without bumping the major or minor version.

License

mockall is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, and LICENSE-MIT for details

Acknowledgements

Mockall was not built in a day. JMock was probably the first popular mock object library. Many ports and imitations have been made, including GoogleMock for C++. Mockers, inspired by GoogleMock, was the first attempt to bring the concept to Rust. The now-defunct Mock_derive was the first library to generate mock objects with procedural macros, greatly reducing the user's workload. Mockall also uses proc macros, and copies many of Mockers' features and conventions. Mockall also takes inspiration from Simulacrum's internal design, and its technique for mocking generic methods.

Extension points exported contracts — how you extend this code

Bah (Interface)
A trait implemented by a Struct we want to mock
mockall/src/examples.rs
MyIterator (Interface)
(no doc) [2 implementers]
mockall/tests/automock_associated_type_constructor.rs
ViaDebug (Interface)
(no doc) [1 implementers]
mockall/src/lib.rs
Foo (Interface)
(no doc) [2 implementers]
mockall/tests/automock_constructor_impl_trait.rs
ViaNothing (Interface)
(no doc) [1 implementers]
mockall/src/lib.rs
Tr (Interface)
Docs for a real (not-mock) trait
mockall/tests/mock_docs.rs
AnyExpectations (Interface)
(no doc)
mockall/src/lib.rs
AsRefMut (Interface)
(no doc) [1 implementers]
mockall/tests/mock_concretize_with_bounds.rs

Core symbols most depended-on inside this repo

times
called by 79
mockall/src/lib.rs
expect
called by 77
mockall_derive/src/mock_function.rs
assert_contains
called by 25
mockall_derive/src/lib.rs
format
called by 18
mockall_derive/src/lib.rs
doc
called by 15
mockall_derive/src/lib.rs
lifetimes_to_generics
called by 14
mockall_derive/src/lib.rs
check_supersuperfy
called by 13
mockall_derive/src/lib.rs
checkpoint
called by 11
mockall_derive/src/mock_function.rs

Shape

Function 539
Method 153
Class 122
Interface 109
Enum 6

Languages

Rust100%

Modules by API surface

mockall_derive/src/lib.rs128 symbols
mockall_derive/src/mock_function.rs52 symbols
mockall/src/lib.rs39 symbols
mockall/tests/mock_struct.rs32 symbols
mockall_derive/src/mockable_struct.rs28 symbols
mockall_derive/src/mock_item_struct.rs17 symbols
mockall_double/src/lib.rs16 symbols
mockall_derive/src/automock.rs13 symbols
mockall/tests/raw_identifier.rs10 symbols
mockall/tests/mock_reference_arguments.rs9 symbols
mockall/tests/automock_qself.rs9 symbols
mockall/tests/automock_must_use.rs9 symbols

For agents

$ claude mcp add mockall \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page