stub_trait

Crates.iostub_trait
lib.rsstub_trait
version1.1.0
sourcesrc
created_at2022-09-14 21:47:21.809332
updated_at2022-12-16 18:40:18.500132
descriptionMacro to implement stub object for a trait.
homepage
repositoryhttps://github.com/leroyguillaume/stub_trait
max_upload_size
id666133
size149,607
Guillaume Leroy (leroyguillaume)

documentation

https://docs.rs/stub_trait

README

ci publish

stub_trait

Macro to implement stub object for a trait.

Overview

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.

Usage

stub_trait is generally only used by tests. Add the following snippet into your Cargo.toml:

[dev-dependencies]
stub_trait = "1.1.0"

You can use it like this:

#[cfg(test)]
use stub_trait::stub;

#[cfg_attr(test, stub)]
trait Animal {
    fn feed(&self, quantity: usize) -> &str;
}

#[cfg(test)]
fn test() {
    let animal = StubAnimal::new().with_stub_of_feed(|i, quantity| {
        if i == 0 {
            assert_eq!(quantity, 10);
            "sad!"
        } else if i == 1 {
            assert_eq!(quantity, 20);
            "happy!"
        } else {
            panic!("too much invocations!")
        }
    });
    assert_eq!(animal.feed(10), "sad!");
    assert_eq!(animal.feed(20), "happy!");
    assert_eq!(animal.count_calls_of_feed(), 2);
}

Contributing

See CONTRIBUTING.md file.

Commit count: 22

cargo fmt