Expand description
Spy crate is inspired by such famous Javascript testing tools as Jasmine and Sinon.js. It provides easy configurable spies with predefined behaviour.
§Spy without arguments
spy!()
creates a pair of a spy function that receives no arguments
and does nothing and corresponded Spy
object.
#[macro_use]
extern crate spy;
use spy::*;
fn main() {
let (spy_fn, spy) = spy!();
assert_eq!(spy_fn(), ());
assert_eq!(spy_fn(), ());
// take a snapshot
let snapshot = spy.snapshot();
// make assertions
assert!(
snapshot.called_with(()),
"should be called with () at least once"
);
assert!(
snapshot.each_called_with(()),
"should be called with () each time"
);
assert_eq!(snapshot.num_of_calls(), 2, "should be called 2 times");
assert_eq!(snapshot.all_calls(), &vec![(), ()]);
assert_eq!(snapshot.first_call().expect("should be Some"), &());
assert_eq!(snapshot.last_call().expect("should be Some"), &());
assert_eq!(snapshot.nth_call(1).expect("should be Some"), &());
}
§Spy with arguments
If a spy function should receive arguments it can be created in one of following ways:
-
spy!(|n, m|)
creates a spy function that can receive two arguments. Any number of arguments could be passed inside||
. Types of these arguments will be inferred from a spy function usage. This function does nothing and returns()
. -
spy!(|n, m| a + b)
creates a spy function that can receive two arguments followed by a function body but any number of arguments could be requested. This syntax is pretty much the same as Rust closures. Types of arguments and an output will be inferred from a spy function usage. -
spy!(|n: u8, m: u8| -> u8 { return n + m; })
creates spy function with type annotations both for arguments and for an output.
Macros§
- spy
- The macro that creates a pair of a
Spy
object and a spy function tracked by returned spy.
Structs§
- Spy
- Spy object that tracks calls of associated spy function.
- SpySnapshot
- The structure represents a snapshot of a spy object. Taken snapshot
contains all the calls starting from the moment when
Spy
object was created or previous snapshot if there was taken one.