Need help with mockall?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

asomers
571 Stars 24 Forks Other 821 Commits 14 Opened issues

Description

A powerful mock object library for Rust

Services available

!
?

Need anything else?

Contributors list

# 50,271
Rust
rust-bi...
unix
libc
725 commits
# 372,873
Rust
jsonweb...
C++
Shell
6 commits
# 407,968
Rust
Shell
C#
aspnetc...
2 commits
# 156,975
kotlin-...
kafka
gnuplot
Dart
1 commit
# 260,087
Rust
C#
Shell
Azure
1 commit
# 72,506
micropa...
Rust
HTML
macOS
1 commit
# 235,231
Django
Shell
linkerd
Scala
1 commit
# 124,108
Shell
tokio
test-fr...
testing...
1 commit
# 433,399
HTML
Shell
Rust
1 commit
# 84,064
github-...
Markdow...
React
Electro...
1 commit
# 29,596
Rust
high-si...
mojave
macOS
1 commit
# 355,651
Rust
kde
Homebre...
C
1 commit
# 92,410
Rust
abac
Shell
actix-w...
1 commit

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.10.2"

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.