Mocking framework for Rust (currently only nightly)

. See documentation for more.

mod hello_world {
    pub fn world() -> &'static str {

    pub fn hello_world() -> String {
        format!("Hello {}!", world())

fn mock_test() {
    hello_world::world.mock_safe(|| MockResult::Return("mocking"));

    assert_eq!("Hello mocking!", hello_world::hello_world());

Collection of the latest Issues




The new version seems to break every existing implementation - furthermore, it's force user to change existing code to suits the mockable behaviour, this is wrong IMHO.

code change: Capture d’écran 2021-10-18 à 14 16 01

(If I move those 2 function into another impl block that doesn't have the macros mockable that make everything compile, but I thought you would like to know that for some complex block there is some problem)




I have 2 creates in my project, base and service, and service crate based on base. And the code is something like this:

I expect db.get_users() is not called, but mock result returns, but it actually didn't respect the mock and goes to expect the real db.get_users() code. Does mocktopus support mocking on method in different crate or I made mistakes?

Thanks a lot




Trying to mock an unsafe function results in an error. Is there any particular reason my unsafe function cannot be mocked?

Minimal test:





Mocks taking effect in every thread during test runtime. Probably should block other test runs, preferably only multithreaded




Mocktopus is very handy for mocking free functions. But when mocking structs, it requires a real copy of the struct to be instantiated. That's not always easy or possible in test environments. That's why most mocking libraries work in terms of traits instead. It would be great if Mocktopus could provide a macro that would instantiate a trait and mock all of its methods. Something like this:

The mock macro could expand to this:




Hey, what do you think of adding a completly new API to improve readability of mockings. I would suggest something like this:

A call to expected would be optional and defaulting to times(1). Other parameter values could be at(2), al_least(3) etc.

called_with would be a method accepting a hamcrest2 matcher for each parameter. Internal we could simply test these with assert_that!(expected_value, <matcher>). This method call would also be optional, defaulting to empty parameter list.

returns will simply specify, what the function returns. Alternatively runs_original() will delegate the call to the original implementation.

The only thing what I need to call this a full mocking library would be the ability to mock the behavior for struct instances, like:

It would be a nice feature to be able to configure (via parameter, function call, etc) if calls not specified in expected raise an error and let the test fail, like:

For struct instances, you could specify with anything like the following, where the test would fail on any call to a method of that struct instance.

These are just some ideas inspired by Mockito, PHPUnit and similar mocking libraries. What do you think of it? Would it be something you want to add in this crate?

I would love to help and contribute to your project if so.


Find the latest versions by id

0.7.4 - Nov 17, 2019

  • Fix UB while copying function input parameters (thank you @artemii235!)

0.7.3 - Jul 23, 2019

  • Fix warnings and UB on newer compilers

0.7.2 - Jun 13, 2019

  • Add MockContext (thank you @czipperz!)

0.7.1 - May 23, 2019

  • Add function for clearing all mocks (thank you @clinedome-work!)

0.7.0 - Feb 13, 2019

  • Allow mock closures to call mockable and mocked functions and to create mocks
  • Make functions not mocked during execution of their mock closures to prevent recursive calls
  • Accept inability to inject modules in separate files: remove tests and fix docs
  • Upgrade project to Rust 2018
  • Add mocking tricks to docs

0.6.0 - Feb 09, 2019

  • Fix for rustc 1.31.0-nightly (ad6e5c003 2018-09-28) and newer (thank you @asomers and @rye!)

0.5.0 - Jul 17, 2018

  • Fix for rustc 1.29.0-nightly (1ecf6929d 2018-07-16) and newer

0.4.1 - Jul 15, 2018

  • Fix tests for rustc 1.29.0-nightly (254f8796b 2018-07-13) and newer

0.4.0 - May 23, 2018

This version is fixed to run with rustc 1.28.0-nightly (71e87be38 2018-05-22) and newer

0.3.8 - May 13, 2018

This version adds mocking with mutable closure

0.3.7 - May 13, 2018

This version changes:

  1. Fix uninitialized memory access when fn arg has drop and mock returns continue
  2. Fix specific case of injecting trait impl

0.3.6 - May 10, 2018

This version changes:

  1. Fixed injecting of fns with unused generics
  2. Prevent injecting of Drop::drop
  3. Upgrade minimum Syn version to 0.13.7

0.3.5 - May 08, 2018

This release makes possible mocking of code in a no_std crate when std lib is available

0.3.4 - May 06, 2018

This version makes annotating items unparsable for Syn raise warning and continue without making it mockable instead of failing whole compilation.

0.3.3 - May 04, 2018

This version prevents macros from injecting unsafe functions, which would make them uncompilable

0.3.2 - May 04, 2018

This version fixes segfault when fn arg has drop and mock is returned, and also fixes returning mocks of generic types, which was broken in version 0.3.1

0.3.1 - May 03, 2018

This version fixes injecting trait impls when return type has longer lifetime than required by trait

0.3.0 - Apr 30, 2018

This release upgrades syn to 0.13.4 and makes it use nightly features of proc-macro2. This lets compiler correctly point at errors in injected functions, which closes issue

0.2.1 - Apr 28, 2018

This release adds support for proc_macros feature being split up into proc_macros and proc_macro_mod in Rust nightly from 2018-04-27

0.2.0 - Jan 23, 2018

This release has no obvious impact on the users. The only change is migration of Mocktopus macros from Syn 0.11 to 0.12 and some code refactoring.

0.1.2 - Jan 23, 2018

This release introduces some bug fixes and no big changes.


  1. Panicking inside mock code does not cause dropping of uninitialized memory
  2. Items with two #[mockable] attributes on them are not injected twice
  3. extern crate mocktopus can be now aliased

0.1.0 - Jan 23, 2018

0.1.1 - Sep 05, 2017

Information - Updated Dec 14, 2021

Stars: 200
Forks: 10
Issues: 14

Rust bindings for libinjection

Add libinjection to dependencies of Cargo

Rust bindings for libinjection

Rust bindings for the C++ api of PyTorch

LIghtweight wrapper for pytorch eg libtorch in rust

Rust bindings for the C++ api of PyTorch

Rust leveldb bindings

Almost-complete bindings for leveldb for Rust

Rust leveldb bindings

rust-analyzer is a modular compiler frontend for the Rust language

It also contains some tips &amp; tricks to help you be more productive when using rust-analyzer

rust-analyzer is a modular compiler frontend for the Rust language

Rust-Lightning is a Bitcoin Lightning library written in Rust

lightning, does not handle networking, persistence, or any other I/O

Rust-Lightning is a Bitcoin Lightning library written in Rust

Rust FUSE - Filesystem in Userspace

Rust library crate for easy implementation of Crate documentation

Rust FUSE - Filesystem in Userspace

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code:

Some images of the test scenes are shown below, but you can find more

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code:

A powerful mock object library for Rust

Mock objects are a powerful technique for unit testing software

A powerful mock object library for Rust


HTTP mocking for Rust!

Before upgrading, make sure to check out the rustfmt as a general code style

HTTP mocking for Rust!


HTTP mocking to test Rust applications

wiremock provides HTTP mocking to perform black-box testing of Rust applications that

HTTP mocking to test Rust applications


HTTP mocking library for Rust

Advanced verification and debugging support

HTTP mocking library for Rust
Facebook Instagram Twitter GitHub Dribbble