Crate mocktoffel

Source
Expand description

This crate provides the capability of mocking local types easily without resorting to creating the mock by hand or having to create mocks for each test.

The crate provides macros for mocking various constructs and will generate a new type for each mock. This ensures that the mock doesn’t intefere with other test code that requires the original type (such as testing the type in question) and yet is available for all tests.

The toffel macro is the initiator that will replace all the mock files with their respective macro types.

The mock macro will generate mocks for the structs or enums it is defined on. This will create a new type that mirrors the type.

The mocks generated are not test-gated at the moment. This may change in a future release. It is expected that the user wraps it in the test feature gate.

use mocktoffel::{toffel, mock};

// #[toffel]
pub struct Foo {
    #[mocked]
    bar: Bar,
    baz: String
}

#[mock]
pub struct Bar {
    some: String,
     
    #[mocked_with(Ok(1))]
    thing: Result<i32, ()>
}

     

Attribute Macros§

mock
The mock macro will generate the corresponding mock for the struct or enum. In future this will support mocking other types as well. The macro generates a new type named <Name>Mock that will match the existing type with the same fields. The type is not test-gated at the moment, so the user is expected to wrap the macro call in a feature gate.
mock_impl
A helper macro that substitutes the original type with the mocked type on implementations and trait implementations. The macro will take care of the occurrence of the type arguments and return types.
toffel
The toffel macro is the initiator which will replace the fields with their corresponding mock types. The fields need to be marked with the #[mocked] attribute. The fields without the attribute will be retained as original.