[][src]Attribute Macro covers::mock

#[mock]

Marks the following function to be built only for testing purposes

In other words it is prepended with #[cfg(any(debug_assertions, test))].

  • It is very useful to not compile mock functions for release.
  • It makes function public - Can be disabled with features = ["no-pub"]
  • It is strictly needed when we use reference to original logic of the mocked function.

Example:

#[mocked(mock_bar)]
fn bar(name: &str) -> String {
    format!("Response: Bar = {}", name)
}

#[mock]
pub fn mock_bar(name: &str) -> String {
    let original_function_result = _bar(name);
    format!("Response: Mocked({})", original_function_result)
}