Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nathanielsimard/mock-it
This library aims to make mocking reliable
https://github.com/nathanielsimard/mock-it
mock rust testing
Last synced: 7 days ago
JSON representation
This library aims to make mocking reliable
- Host: GitHub
- URL: https://github.com/nathanielsimard/mock-it
- Owner: nathanielsimard
- License: mit
- Created: 2018-03-08T03:01:51.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2022-10-18T14:49:32.000Z (about 2 years ago)
- Last Synced: 2024-10-03T12:25:49.845Z (about 1 month ago)
- Topics: mock, rust, testing
- Language: Rust
- Homepage:
- Size: 79.1 KB
- Stars: 41
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mock-it
[![Current Crates.io Version](https://img.shields.io/crates/v/mock-it.svg)](https://crates.io/crates/mock-it)
> 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
``` rust
#[cfg_attr(test, mock_it::mock_it)]
trait Nurse {
fn heal(&self, pokemon: Pokemon) -> Result;
}#[derive(Debug, PartialEq, Clone)]
pub struct Pokemon {
hp: i32,
}struct PokemonCenter {
nurse: Box,
pokemons: Vec,
}impl PokemonCenter {
pub fn accept(&mut self, pokemon: Pokemon) {
self.pokemons.push(pokemon);
}pub fn collect(&mut self) -> Result {
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](/examples)## Constraints
* Trait inputs must implement both PartialEq and Clone
* Trait ouput must implement Clone