1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! A Rust mocking framework for standalone functions and methods in an impl block.
//!
//! fnmock lets you replace or observe a function's behaviour in tests without introducing a trait
//! or dependency injection wiring. You annotate the function where it already lives, and the test
//! either controls what it returns ([`fakeable`]) or asserts on how it was called ([`spyable`]).
//!
//! # Fakes
//!
//! [`fakeable`] injects a lookup at the top of the function: if the test installed a fake, the
//! fake runs instead of the body.
//!
//! ```ignore
//! #[fnmock::fakeable]
//! fn fetch_user_name(id: u32) -> String {
//! todo!()
//! }
//!
//! fn greet(id: u32) -> String {
//! format!("Hello, {}", fetch_user_name(id))
//! }
//!
//! #[test]
//! fn test_greeting() {
//! fetch_user_name_fake().setup(|_| "Test".into());
//!
//! assert_eq!(greet(1), "Hello, Test");
//! }
//! ```
//!
//! # Spies
//!
//! [`spyable`] works the other way round: the real body still runs, and the test asserts on the
//! arguments it was called with.
//!
//! ```ignore
//! #[fnmock::spyable]
//! fn fetch_user_name(id: u32) -> String {
//! format!("user {id}")
//! }
//!
//! #[test]
//! fn test_fetch_user_name() {
//! let spy = fetch_user_name_spy();
//! spy.expect(fnmock::predicate::eq(1)).once();
//!
//! assert_eq!(fetch_user_name(1), "user 1"); // the real body still runs
//!
//! spy.assert();
//! }
//! ```
//!
//! `expect` and `expectf` hand back an [`ExpectationHandle`], which refines the expectation by
//! chaining: `times` (taking anything that converts into a [`CallRange`] — a count or a range),
//! `once`, `never`, `describe` and `in_sequence`, where a [`Sequence`] orders expectations across
//! several spies.
//!
//! # Crate layout
//!
//! [`fakeable`] and [`spyable`] are the whole API you apply to production code. The types a test
//! touches are [`ExpectationHandle`], [`CallRange`], [`Sequence`] and the re-exported
//! [`predicate`] builders. Every other module — the fake and spy stores, matchers, expectations —
//! only exists because the code the macros expand to has to name it; they are fnmock internals and
//! you should not interact with them directly.
//!
//! Fakes and spies are kept per thread and their lookup is `#[cfg(test)]`-gated, so each `#[test]`
//! gets its own isolated state and release builds compile no fake or spy machinery at all. The
//! flip side is that both can only be set up from a `#[cfg(test)]` unit test inside the crate that
//! defines the annotated item.
//!
//! See the [README](https://github.com/jakob-rzeppa/fnmock/blob/master/README.md) for installation,
//! a walkthrough and the current limitations.
// Re-export the public API to make doc-comments visible.
pub use CallRange;
pub use ExpectationHandle;
pub use Sequence;
pub use ;
/// Re-export the derive macro so that users of the library can just use `fnmock::fakeable` instead of having to depend on `fnmock-derive` directly.
pub use *;