Crates.io | default-test |
lib.rs | default-test |
version | 0.1.1 |
source | src |
created_at | 2021-01-06 04:36:03.788208 |
updated_at | 2021-01-06 04:39:12.411966 |
description | A default trait that can be used in tests |
homepage | https://github.com/austinjones/default-test-rs |
repository | https://github.com/austinjones/default-test-rs |
max_upload_size | |
id | 332873 |
size | 12,352 |
Provides a Rust trait similar to Default that can be used in tests.
Often tests need to construct mock instances of structs. For example:
struct User {
id: usize,
name: String,
email: String,
admin: bool
}
While it's tempting to define Default, often tests need mock values for types that shouldn't apply in production code. Sometimes, tests need values for types that don't even implement Default.
This crate provides DefaultTest, a trait which can provide default instances with mock values.
impl DefaultTest for User {
fn default_test() -> Self {
User {
id: 0,
name: "name".into(),
email: "email".into(),
admin: false
}
}
}
Unit tests can then use the spread operator to construct values:
mod tests {
#[test]
fn test() {
let user = User {
id: 99
..User::test_default()
};
// ...
}
}