#mock #mocking #stub #testing #stubing

macro stub_trait

Macro to implement stub object for a trait

2 unstable releases

Uses new Rust 2021

0.2.0 Sep 15, 2022
0.1.0 Sep 14, 2022

#221 in Testing

40 downloads per month

MIT license

14KB
156 lines

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.

Dependencies

~220–630KB
~15K SLoC