Crates.io | test_double |
lib.rs | test_double |
version | 0.3.0 |
source | src |
created_at | 2018-05-01 22:03:10.340476 |
updated_at | 2020-02-22 02:30:04.61265 |
description | Procedural macro for substituting one type for another when testing. |
homepage | |
repository | https://github.com/pcsm/test_double |
max_upload_size | |
id | 63331 |
size | 24,919 |
A set of procedural macros that can swap in mock objects, dummy objects, or other test doubles only when testing.
This doesn't solve the problem of mocking in the first place, which is worth looking into, but it does allow you to import mock objects into your tests easily.
To use, add the following to your Cargo.toml
:
[dependencies]
test_double = "0.3.0"
Note that this crate has not yet reached version 1.0, so the API may change between releases.
#[test_double]
can be used to substitute one type for another when testing:
#[test_double]
use db::Database;
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::DatabaseMock as Database;
The substituted type name defaults to the original name, with Mock
appended to it.
You can also provide an alternate name to be substituted instead:
#[test_double(DummyDB)]
use db::Database;
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::DummyDB as Database;
Note that this is only supported when substituting a single type name.
As a shortcut, if you would like to use the original type name, prefixed with Mock
instead of appended, you can use the #[test_double_prefixed]
macro:
#[test_double_prefixed]
use db::Database;
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::MockDatabase as Database;
It's worth noting that these macros also support grouped imports:
#[test_double]
use db::{
Database,
Connection
};
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::DatabaseMock as Database;
#[cfg(not(test))]
use db::Connection;
#[cfg(test)]
use db::ConnectionMock as Connection;
For #[test_doubles]
and #[test_doubles_prefixed]
:
use blah::*;
, are not supporteduse blah::{foo, bar};
, are not supportedIf you'd like to substitute multiple use statements at once, you can use the test_doubles!
macro. Note that this macro does not support using an alternate substituted name, but it does support grouped imports, such as use blah::{foo, bar};
.
test_doubles! {
use db::{
Database,
Connection
};
use image::ImageCache;
}
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::DatabaseMock as Database;
#[cfg(not(test))]
use db::Connection;
#[cfg(test)]
use db::ConnectionMock as Connection;
#[cfg(not(test))]
use image::ImageCache;
#[cfg(test)]
use image::ImageCacheMock as ImageCache;
Similar to the single-use-statement macros, there is a test_doubles_prefixed!
macro that can prefix instead of appending:
test_doubles_prefixed! {
use db::Database;
use image::ImageCache;
}
// Expands to:
#[cfg(not(test))]
use db::Database;
#[cfg(test)]
use db::MockDatabase as Database;
#[cfg(not(test))]
use image::ImageCache;
#[cfg(test)]
use image::MockImageCache as ImageCache;
For test_doubles!
and test_doubles_prefixed!
:
use blah::*;
, are not supported