13 releases (8 breaking)

Uses new Rust 2021

0.9.0 Jul 13, 2022
0.7.0 Jun 28, 2022
0.3.0 May 17, 2019
0.2.1 Sep 23, 2018
0.1.0 Mar 11, 2018

#99 in Testing

Download history 157/week @ 2022-08-19 294/week @ 2022-08-26 289/week @ 2022-09-02 337/week @ 2022-09-09 301/week @ 2022-09-16 239/week @ 2022-09-23 231/week @ 2022-09-30 271/week @ 2022-10-07 293/week @ 2022-10-14 195/week @ 2022-10-21 240/week @ 2022-10-28 201/week @ 2022-11-04 169/week @ 2022-11-11 220/week @ 2022-11-18 222/week @ 2022-11-25 142/week @ 2022-12-02

774 downloads per month
Used in table-test

MIT license

18KB
425 lines

mock-it


Current Crates.io Version

Mock it, don't mock all 🙃

Our goal is to enhance the DX behind mocking your depedencies when you test. It lets you use a syntax closer to given when then instead of having to assert your then BEFORE you call your function.

Features

  • Intuitive usage 😌
  • Mock your traits 🦾
  • Configure your mocks 👷‍♀️
  • Separate configuration from assertion 🕵️‍♀️

Example

#[cfg_attr(test, mock_it::mock_it)]
trait Nurse {
    fn heal(&self, pokemon: Pokemon) -> Result<Pokemon, String>;
}

#[derive(Debug, PartialEq, Clone)]
pub struct Pokemon {
    hp: i32,
}

struct PokemonCenter {
    nurse: Box<dyn Nurse>,
    pokemons: Vec<Pokemon>,
}

impl PokemonCenter {
    pub fn accept(&mut self, pokemon: Pokemon) {
        self.pokemons.push(pokemon);
    }

    pub fn collect(&mut self) -> Result<Pokemon, String> {
        let pokemon = match self.pokemons.pop() {
            Some(val) => val,
            None => return Err("No pokemon".to_string()),
        };
        self.nurse.heal(pokemon)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mock_it::{any, eq};

    #[test]
    fn can_heal_pokemon() {
        // given
        let pikachu_no_hp = Pokemon { hp: 0 };
        let pikachu_full_hp = Pokemon { hp: 100 };

        let nurse_joy = NurseMock::new();
        nurse_joy.when_heal(eq(pikachu_no_hp.clone()))
            .will_return(Ok(pikachu_full_hp.clone()));

        let mut pokemon_center = PokemonCenter {
            nurse: Box::new(nurse_joy.clone()),
            pokemons: vec![],
        };

        // when
        pokemon_center.accept(pikachu_no_hp);
        let healed_pikachu = pokemon_center.collect().unwrap();

        //then
        assert_eq!(healed_pikachu, pikachu_full_hp);
        assert!(nurse_joy.expect_heal(any()).times(1).called());
    }
}

Further use cases

Constraints

  • Trait inputs must implement both PartialEq and Clone
  • Trait ouput must implement Clone

Dependencies

~210–610KB
~15K SLoC