skip to content

Property Tests and Mocks

proptest generates inputs and shrinks failures, and mockall builds mocks from traits. Interviewers ask how to make Rust code testable when there is no runtime reflection.

on this pageshow

questions

5

A Rust rate limiter calls `Instant::now()` directly and a reviewer asks for a mockall test: why can't the call be mocked in place, and should the seam be generic or `Box<dyn Clock>`?

level: seniorimportance: must knowfreq 36%

answer

  1. no run-time hook on a named call
  2. mockall writes a new type
  3. the trait is the seam
  4. monomorphised call or vtable call
  5. lend the mock, then checkpoint

basics

~20 s

Rust cannot swap a directly named function at run time, and mockall only generates a new type, so the limiter must take a Clock trait. A generic C: Clock costs nothing at run time; a Box<dyn Clock> keeps one concrete type.

solid answer

~40 s

`Instant::now()` is a path the compiler resolves at build time, and Rust has no mechanism to swap a function's body at run time, so no crate can intercept it. mockall generates code instead: `#[automock]` writes a `MockClock` type, which only helps code that accepts a trait. So the refactor is a `Clock` trait, a `SystemClock` that wraps `Instant`, and a bucket that takes a `Clock`. A generic `TokenBucket<C: Clock = SystemClock>` is monomorphised, the call can be inlined, and the default type parameter keeps production signatures short; tests can even lend `&MockClock` through a forwarding impl and call `checkpoint()` afterwards. A `Box<dyn Clock + Send>` field gives one concrete type and run-time choice, costs a vtable call, needs a dyn-compatible trait, and owns the mock, so counts are verified when the bucket drops.

code

rust · 29 lines
rust
pub trait Clock {
    fn now_ms(&self) -> u64;
    fn sleep_ms(&self, ms: u64);
}

pub struct SystemClock {
    start: std::time::Instant,
}

impl Clock for SystemClock {
    fn now_ms(&self) -> u64 { self.start.elapsed().as_millis() as u64 }
    fn sleep_ms(&self, ms: u64) { std::thread::sleep(std::time::Duration::from_millis(ms)) }
}

// Generic seam: the clock's type is part of the bucket's type.
pub struct TokenBucket<C: Clock = SystemClock> {
    pub clock: C,
}

// Object seam: one concrete type, the clock chosen at run time.
pub struct DynBucket {
    pub clock: Box<dyn Clock + Send>,
}

// Lend a clock instead of handing it over.
impl<T: Clock + ?Sized> Clock for &T {
    fn now_ms(&self) -> u64 { (**self).now_ms() }
    fn sleep_ms(&self, ms: u64) { (**self).sleep_ms(ms) }
}

go deeper

for a junior

Recall that Rust code becomes mockable by taking a trait, and that mockall generates a mock type implementing that trait.

for a middle

Explain why a direct call such as Instant::now() cannot be swapped at run time, and how a generic parameter or a Box<dyn Trait> field lets a test pass a mock instead.

for a senior

Choose the seam for the code in front of you: generic with a default type for zero cost and lendable mocks, a trait object for one concrete type, and know what each does to verification timing.

for a principal

Set where seams belong across a codebase so that test control is gained without a trait for every struct or type parameters spreading through every layer.

## Why the call cannot be intercepted `Instant::now()` is a path the compiler resolves while building the crate. The language has no mechanism to replace a function's body at run time: no monkey-patching and no generated run-time proxies. `std::any::Any` can identify a value's type, but it cannot redirect a call. Whatever a test wants to substitute must therefore be substituted at compile time. **mockall** works within that limit. Its `#[automock]` attribute writes a new struct, `MockClock`, that implements a trait. A new type helps only where code accepts *some* implementation of that trait. mockall can also mock a struct of your own, by putting `#[automock]` on its single `impl` block and swapping the import path under `cfg(test)` with the companion crate mockall_double's `#[double]`, but that still changes which type your code names. It cannot reach into `std::time::Instant`. So the refactor comes first: 1. Declare a `Clock` trait with the operations the limiter needs: `now_ms()` and `sleep_ms(ms)`. 2. Implement it for a `SystemClock` that wraps `Instant` and `std::thread::sleep`. 3. Make the limiter accept a `Clock` instead of calling `Instant::now()`. 4. Test with `MockClock`, or with a hand-written fake when time has to advance. ## Two places to put the seam | | Generic `TokenBucket<C: Clock>` | `Box<dyn Clock + Send>` field | |---|---|---| | Dispatch | monomorphised static call, can be inlined | vtable call through the box | | Type seen by callers | `TokenBucket<SystemClock>`, `TokenBucket<MockClock>`... | always `DynBucket` | | Trait requirements | any trait, even with generic methods | must be dyn compatible | | Extra bounds | only what you write | `Send` from the type, `'static` by default | | What a test can do | lend `&MockClock` and inspect it afterwards | hand the mock over; counts checked when the box drops | Generics and trait objects in general, dispatch cost and dyn compatibility, have their own material; the testing consequences are the ones below. ## What the generic seam gives a test - **A default type parameter keeps production code tidy.** With `TokenBucket<C: Clock = SystemClock>`, a struct field or function parameter written as plain `TokenBucket` means `TokenBucket<SystemClock>`, so the test seam does not leak into every signature. Construction infers `C` from the argument. - **Lending instead of handing over.** A blanket `impl<T: Clock + ?Sized> Clock for &T` makes any borrowed clock a clock. The test builds `TokenBucket::new(&clock, ...)`, lets the bucket go out of scope, and then calls `clock.checkpoint()` to verify the counts at that point and clear them. Calling `checkpoint()` while the bucket is still used afterwards is rejected with E0502, "cannot borrow `clock` as mutable because it is also borrowed as immutable": the borrow checker enforces the order. `TokenBucket` has no `Drop` impl, so the borrow ends at the bucket's last use; the inner block in the test only makes that visible. - **Generic trait methods stay mockable.** mockall mocks a method like `fn read<T: 'static>(&self) -> T` with a turbofish on the expectation, `expect_read::<u64>()`. Such a trait is not dyn compatible, so only a generic seam can hold it. ## What the object seam gives a test - **One concrete type.** `DynBucket` has no type parameter, which helps when the clock is chosen from configuration at run time, or when a parameter would otherwise spread through many layers of an application. - **The mock moves into the box.** mockall mocks are always `Send` and are `'static` when they borrow nothing, so `Box::new(clock)` satisfies `Box<dyn Clock + Send>`, and the bucket can even move into a spawned thread. Verification then happens when the box drops; an explicit `drop(bucket)` puts that moment where you want it. - **The trait must stay dyn compatible.** Adding a generic method later breaks every `Box<dyn Clock>`, so the object seam constrains how the trait may grow. ## Seams that cost more than they look - **Static methods and module functions.** mockall can mock an associated function without `self`, and `#[automock]` on a module of free functions generates a `mock_<name>` module to import with `#[double]`. Either way the expectations are global, set through a per-function context object such as `foo_context()` for a static `foo`. The test harness runs tests on parallel threads, so mockall's docs tell you to provide your own synchronisation. An instance method on an injected value avoids the problem. - **A trait per struct.** Introducing a trait only so that something can be mocked makes every collaborator generic or boxed. Seams belong where tests genuinely need control: time, I/O, randomness. For this reviewer's request the generic seam with a `SystemClock` default is the usual answer. It changes no production call sites, costs nothing at run time, and lets the test lend its mock.

  • What does the default type parameter in TokenBucket<C: Clock = SystemClock> buy you, and what does it not do?
    In type positions, such as a struct field or a function parameter, plain `TokenBucket` means `TokenBucket<SystemClock>`, so production code never spells the clock out. It does not choose the clock when you construct a bucket: `TokenBucket::new(clock, ...)` infers `C` from the argument, which is why tests passing a `MockClock` need no annotation.
  • With the mock boxed inside DynBucket, how do you check its call counts before the test ends?
    You cannot call `checkpoint()` on a mock the box owns unless the type exposes it. Either `drop(bucket)` at the point where the counts should hold, which runs the mock's drop-time check there, or switch to a generic seam and lend `&MockClock`, so the test keeps ownership and calls `checkpoint()` after the bucket's last use.
  • Could a Clock trait with a generic method still be mocked, and which seam could hold it?
    Yes. mockall mocks generic methods whose type parameters are `'static`, and the expectation takes a turbofish, as in `expect_read::<u64>()`. A trait with a generic method is not dyn compatible, so `Box<dyn Clock>` stops compiling; only a generic seam can hold the mock.

  • mockall can patch Instant::now() for the length of one test
  • Only a trait object can receive a mock; generic code cannot
  • A generic clock parameter costs a virtual call on every read
  • Box<dyn Clock> accepts any trait, even one with generic methods
  • Mocking in Rust needs unsafe code or a nightly compiler
open as a page

In Rust, what does mockall's `#[automock]` on a `Clock` trait generate, and what do `expect_now_ms()`, `times`, `returning` and `with` configure?

level: middleimportance: should knowfreq 30%

basics

~10 s

#[automock] generates MockClock, a struct implementing Clock. expect_now_ms() adds an expectation; returning or return_const sets its result, with or withf matches arguments, and times sets a count checked at each call and at drop.

open as a page

In Rust's proptest crate, what does a `proptest!` test over strategies like `1u32..=20` and `prop::collection::vec` run, and why use `prop_assert!`?

level: middleimportance: should knowfreq 32%

basics

~20 s

proptest! rewrites each fn name(x in strategy) into a zero-argument test that draws inputs from the strategies, runs the body until 256 cases pass by default and shrinks any failure; prop_assert! fails a case by returning an error, not panicking.

open as a page

A Rust proptest run reports `minimal failing input: capacity = 1, refill_ms = 1, idle_ms = 1` and writes `proptest-regressions/lib.txt`: how did it shrink, and what does that file hold?

level: seniorimportance: should knowfreq 22%

basics

~20 s

proptest shrank by simplifying each input's value tree, binary-searching integers toward each range's low end while the case kept failing. The file stores that case's RNG seed, replayed before new cases on every run, so commit it.

open as a page

As the lead of a Rust service team, what rule would you set for test seams: generic parameters, trait objects, hand-written fakes or mockall?

level: principalimportance: should knowfreq 18%

basics

~20 s

Put traits only at boundaries tests must control: time, I/O, randomness. Default to a generic seam with a production default type and a shared fake, use mockall where the call itself is the behaviour, and decide which crates see the mocks.

open as a page