Crates.io | stub_trait_core |
lib.rs | stub_trait_core |
version | 0.2.0 |
source | src |
created_at | 2022-09-14 21:41:48.271477 |
updated_at | 2022-09-15 20:07:11.821563 |
description | Core structs used by stub_trait library. |
homepage | |
repository | https://github.com/leroyguillaume/stub_trait |
max_upload_size | |
id | 666131 |
size | 3,215 |
Macro to implement stub object for a trait.
Stub traits is a technique to simulate some comportments or to avoid to be blocked by a specific part of the code that is not implemented yet.
stub_trait is generally only used by tests. Add the following snippet into your Cargo.toml
:
[dev-dependencies]
stub_trait = "0.2.0"
You can use it like this:
#[cfg(test)]
use stub_trait::stub;
#[cfg_attr(test, stub)]
trait Animal {
fn name(&self) -> &str;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn stub_all_calls() {
let mut animal = StubAnimal::default();
animal.stub_all_calls_of_name(|| "Ivana");
assert_eq!(animal.name(), "Ivana");
assert_eq!(animal.name(), "Ivana");
assert_eq!(animal.count_calls_of_name(), 2);
}
#[test]
fn stub_call_by_call() {
let mut animal = StubAnimal::default();
animal.register_stub_of_name(|| "Ivana");
animal.register_stub_of_name(|| "Truffle");
assert_eq!(animal.name(), "Ivana");
assert_eq!(animal.name(), "Truffle");
assert_eq!(animal.count_calls_of_name(), 2);
}
}
See CONTRIBUTING.md file.