#[mock]
Expand description
Injects mocking logic into a function / method.
You may want to use this attribute conditionally, e.g.,
behind a #[cfg_attr(test, _)]
wrapper.
Attributes
Attributes are specified according to standard Rust conventions:
#[mock(attr1 = "value1", ...)]
.
using
Specifies a path string to the mock state. The path can point to the type of the mock state
(e.g., "mocks::State"
); in this case, the mock impl is an inherent function of the state
with the same name as the mocked function / method. Alternatively, a path can specify
the function name as well (e.g., "mocks::State::mock_something"
); this is useful in case
of name collision. The choice of these 2 options is auto-detected based on the last segment
in the path: if it starts with an uppercase letter, it is considered a mock state type;
otherwise, it is considered a type + function.
rename
Specifies a pattern to use when accessing mock impl methods. A pattern is a string with {}
denoting a placeholder for the mocked function name. For example, mock_{}
pattern will
rename len
to mock_len
.
This attribute is mostly useful for impl blocks.
Supported items
The mock
attribute can be used on functions / methods. Pretty much all signatures
are supported, e.g., generic functions, non-'static
args, return types
with dependent / elided lifetime, etc. const
functions are not supported.
The mock
attribute can also be placed on an impl block (including a trait implementation).
In this case, it will apply to all methods in the block. If necessary, mocking options can
be overridden for separate methods in the block by adding a mock
attribute on them.
Examples
See mimicry
docs for examples of usage.