Hello all.
I have written simple prototype of mocking library. It is very basic, very limited and lacks many features, but I want to get your feedback to know whether my approach is usable at all.
Mocking is implemented using compiler plugin, so nighly Rust is required.
Project is located at GitHub: mockers, you can get short introduction there.
Crates (library and compiler plugin) are uploaded to crates.io: mockers, mockers_macros
Usage:
// air/src/lib.rs
pub trait AirConditioner {
fn make_hotter(&self, by: i16);
fn make_cooler(&self, by: i16);
fn get_temperature(&self) -> i16;
}
pub fn set_temperature_20(cond: &airconditioner) {
let t = cond.get_temperature();
if t < 20 {
cond.make_hotter(20 + t);
} else {
cond.make_cooler(t - 20);
}
}
// air/tests/lib.rs
#![feature(plugin)]
#![plugin(mockers_macros)]
extern crate air;
extern crate mockers;
mock!{
AirConditionerMock,
air,
trait AirConditioner {
fn make_hotter(&self, by: i16);
fn make_cooler(&self, by: i16);
fn get_temperature(&self) -> i16;
}
}
use mockers::Scenario;
#[test]
fn test_make_hotter() {
let mut scenario = Scenario::new();
let cond = scenario.create_mock::<AirConditionerMock>();
scenario.expect(cond.get_temperature_call().and_return(16));
scenario.expect(cond.make_hotter_call(4).and_return(()));
mocked::set_temperature_20(&cond);
}
Running tests:
…
---- test_make_hotter stdout ----
thread 'test_make_hotter' panicked at 'called `Result::unwrap()` on an `Err` value: "36 is not equal to 4"', ../src/libcore/result.rs:746
…