stub_trait_core

Crates.iostub_trait_core
lib.rsstub_trait_core
version0.2.0
sourcesrc
created_at2022-09-14 21:41:48.271477
updated_at2022-09-15 20:07:11.821563
descriptionCore structs used by stub_trait library.
homepage
repositoryhttps://github.com/leroyguillaume/stub_trait
max_upload_size
id666131
size3,215
Guillaume Leroy (leroyguillaume)

documentation

https://docs.rs/stub_trait_core

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 = "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);
    }
}

Contributing

See CONTRIBUTING.md file.

Commit count: 22

cargo fmt