Skip to content
Procedural macro for substituting one type for another when testing
Rust
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
src
.gitignore
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md

README.md

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::*;
You can’t perform that action at this time.