#mock #test #testing #TDD

macro test_double

Procedural macro for substituting one type for another when testing

3 unstable releases

✓ Uses Rust 2018 edition

0.2.0 Apr 12, 2019
0.1.1 May 1, 2018
0.1.0 May 1, 2018

#93 in Testing

12 downloads per month

MIT/Apache

13KB
224 lines

test_double Crates.io Rustc Version 1.31+

A procedural macro that can swap in mock objects, dummy objects, or other test doubles only when testing.

To use, add the following to your Cargo.toml:

[dependencies]
test_double = "0.2.0"

Note that this crate has not yet reached version 1.0, so the API may change between releases.

Substituting One Type: #[test_double]

The substituted name defaults to the original name, postfixed with "Mock":

#[test_double]
use image::ImageManager;

// Expands to:

#[cfg(not(test))]
use image::ImageManager;
#[cfg(test)]
use image::ImageManagerMock as ImageManager;

If you'd like to provide an alternate subsituted name, you can do so:

#[test_double(IMDummy)]
use image::ImageManager;

// Expands to:

#[cfg(not(test))]
use image::ImageManager;
#[cfg(test)]
use image::IMDummy as ImageManager;

Limitations

#[test_double] can't be used with:

  • Glob imports, like use blah::*;
  • Grouped imports, like use blah::{foo, bar};, when providing an alternate substituted name

Substituting Multiple Types: test_doubles!

If you'd like to substitute multiple types at once, you can use the function-like macro. Note that this does not support providing an alternate substituted name.

test_doubles! {
    use image::ImageManager;
    use texture::TextureManager;
}

// Expands to:

#[cfg(not(test))]
use image::ImageManager;
#[cfg(test)]
use image::ImageManagerMock as ImageManager;
#[cfg(not(test))]
use texture::TextureManager;
#[cfg(test)]
use texture::TextureManagerMock as TextureManager;

Limitations

test_doubles! can't be used with:

  • Glob imports, like use blah::*;

Dependencies

~785KB
~18K SLoC