5 unstable releases

Uses old Rust 2015

0.2.0 Aug 26, 2018
0.1.0 Feb 4, 2018
0.0.3 Apr 2, 2017
0.0.2 Mar 25, 2017
0.0.1 Mar 24, 2017

#148 in Testing

Download history 282/week @ 2021-02-23 158/week @ 2021-03-02 257/week @ 2021-03-09 270/week @ 2021-03-16 288/week @ 2021-03-23 557/week @ 2021-03-30 423/week @ 2021-04-06 388/week @ 2021-04-13 402/week @ 2021-04-20 210/week @ 2021-04-27 312/week @ 2021-05-04 381/week @ 2021-05-11 255/week @ 2021-05-18 135/week @ 2021-05-25 361/week @ 2021-06-01 342/week @ 2021-06-08

1,447 downloads per month
Used in 3 crates

MIT license

15KB
126 lines

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.

No runtime deps