pseudo

Crates.iopseudo
lib.rspseudo
version0.2.0
sourcesrc
created_at2017-03-24 03:27:06.416986
updated_at2018-08-26 21:48:24.280271
descriptionA small mocking library for Rust
homepage
repositoryhttps://github.com/iredelmeier/pseudo
max_upload_size
id9123
size20,121
Isobel Redelmeier (iredelmeier)

documentation

https://docs.rs/pseudo

README

Pseudo

A small mocking library for Rust.

Crates.io Version Docs Build Status

Pseudo lets you mock Trait implementations so that you can track function call arguments and set return values or overrides functions at test time.

Here's a quick example:

extern crate pseudo;

use pseudo::Mock;

trait Foo: Clone {
    fn expensive_fn(&self, x: i64, y: i64) -> i64;
}

#[derive(Clone)]
struct MockFoo {
    pub expensive_fn: Mock<(i64, i64), i64>,
}

impl Foo for MockFoo {
    fn expensive_fn(&self, x: i64, y: i64) -> i64 {
        self.expensive_fn.call((x + 10, y))
    }
}

fn double_expensive_fn<T: Foo>(foo: &T, x: i64, y: i64) -> i64 {
    foo.expensive_fn(x, y) * 2
}

#[test]
fn doubles_return_value() {
    let mock = MockFoo { expensive_fn: Mock::default() };

    mock.expensive_fn.return_value(1000);

    assert_eq!(double_expensive_fn(&mock, 1, 2), 2000);
}

#[test]
fn uses_correct_args() {
    let mock = MockFoo { expensive_fn: Mock::default() };

    assert!(!mock.expensive_fn.called());

    double_expensive_fn(&mock, 1, 2);

    assert_eq!(mock.expensive_fn.num_calls(), 1);
    assert!(mock.expensive_fn.called_with((11, 2)));
}

More examples are available in the examples directory.

Commit count: 22

cargo fmt