https://github.com/iredelmeier/pseudo
A small mocking library for Rust
https://github.com/iredelmeier/pseudo
mock-library rust testing
Last synced: about 1 year ago
JSON representation
A small mocking library for Rust
- Host: GitHub
- URL: https://github.com/iredelmeier/pseudo
- Owner: iredelmeier
- License: mit
- Created: 2017-03-24T02:08:53.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-26T21:50:01.000Z (almost 8 years ago)
- Last Synced: 2025-04-28T12:04:00.247Z (about 1 year ago)
- Topics: mock-library, rust, testing
- Language: Rust
- Homepage:
- Size: 14.6 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pseudo
### A small mocking library for Rust.
[](https://crates.io/crates/pseudo) [](https://docs.rs/pseudo) [](https://travis-ci.org/iredelmeier/pseudo)
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:
```rust
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(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](./examples).