Skip to content

Commit

Permalink
Show Mockall in action
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeisler committed Nov 30, 2023
1 parent efeee84 commit cb3a60b
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
112 changes: 112 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
- [Other Types of Tests](testing/other.md)
- [Useful Crates](testing/useful-crates.md)
- [GoogleTest](testing/googletest.md)
- [Mocking](testing/mocking.md)
- [Compiler lints and Clippy](testing/lints.md)
- [Exercise: Luhn Algorithm](testing/exercise.md)
- [Solution](testing/solution.md)
Expand Down
7 changes: 7 additions & 0 deletions src/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ crate-type = ["staticlib"]
path = "googletest.rs"
test = true

[[example]]
name = "mockall-example"
crate-type = ["staticlib"]
path = "mockall.rs"
test = true

[[bin]]
name = "luhn"
path = "exercise.rs"

[dependencies]
googletest = "0.10.0"
mockall = "0.11.4"
13 changes: 13 additions & 0 deletions src/testing/mockall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::time::Duration;

#[mockall::automock]
pub trait Pet {
fn is_hungry(&self, since_last_meal: Duration) -> bool;
}

#[test]
fn test_robot_pet() {
let mut mock_dog = MockPet::new();
mock_dog.expect_is_hungry().return_const(true);
assert_eq!(mock_dog.is_hungry(Duration::from_secs(10)), true);
}
44 changes: 44 additions & 0 deletions src/testing/mocking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
minutes: 10
---

# Mocking

If you want to do mocking, we recommend [Mockall]. You need to refactor your
code to use traits, which you can then quickly mock:

```rust,ignore
{{#include mockall.rs}}
```

[Mockall]: https://docs.rs/mockall/

<details>

- Mockall is not part of the Rust Playground, so you need to run this example in
a local environment. Use `cargo add mockall` to quickly add Mockall to an
existing Cargo project.

- Mockall has a lot more functionality. In particular, you can set up
expectations which depend on the arguments passed:

```rust,ignore
let mut mock_cat = MockPet::new();
mock_cat
.expect_is_hungry()
.with(mockall::predicate::gt(Duration::from_secs(3 * 3600)))
.return_const(true);
mock_cat
.expect_is_hungry()
.return_const(true);
assert_eq!(mock_cat.is_hungry(Duration::from_secs(1 * 3600)), false);
assert_eq!(mock_cat.is_hungry(Duration::from_secs(5 * 3600)), true);
```
- You can use `.times(n)` to limit the number of times a mock method can be
called to `n` --- the mock will automatically panic when dropped if this isn't
satisfied.
- Mockall is available for use in AOSP.
</details>

0 comments on commit cb3a60b

Please sign in to comment.