#mocking #derive

damock

Derivable data mocking for tests

2 releases

0.1.3 Oct 20, 2024
0.1.2 Oct 15, 2024

#66 in #mocking

Download history 92/week @ 2024-10-10 203/week @ 2024-10-17 39/week @ 2024-10-24 12/week @ 2024-10-31 10/week @ 2024-11-07

305 downloads per month

MIT license

5KB

Damock - Composable Data Mocking

ci_status codecov license crates_io docs_rs

use damock::Mock;

#[derive(Mock)]
struct Foo {
    bar: Bar,
    #[mock_default]
    baz: u8
}

#[derive(Mock)]
enum Bar {
    #[mock]
    A,
    B,
}

The former derive expands into:

// Derived mock implementations will
// always be conditionally compiled.
#[cfg(test)]
impl Mock for Foo {
    fn mock() -> Self {
        Self {
            bar: Mock::mock(),
            baz: Default::default(),
        }
    }
}

Toy application:

#[test]
fn computes_data() {
  let actual = compute(DataInput::mock());
  assert_eq!(DataOutput::mock(), actual);
}

The test compiler configuration may be overridden to something else like so:

#[derive(damock::Mock)]
#[mock(feature = "mocks")]
struct Foo;

This may come in use when Mock implementations need be shared between workspace crates.

Dependencies

~240–680KB
~16K SLoC