Skip to content

Mocking custom function that uses lettre rust create #482

Answered by asomers
Sirneij asked this question in Questions
Discussion options

You must be logged in to vote

You can certainly mock send_email. But unlike in Python, you can't select between the real implementation and the mock implementation at runtime. You must do it at compile time. You can do it by creating a module for your mock functions:

#[automock]
mod mymod {
    pub async fn send_email(...) {unimplemented!()}
}

and then in whatever file send_multipart_email is defined, you can select at compile time whether to use the mock:

#[cfg(test)]
use crate::mock_mymod::send_email;
#[cfg(not(test)]
use some_other_module::send_email;

that's basically what #[double] does, except it makes stronger assumptions about the module naming convention.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@Sirneij
Comment options

@asomers
Comment options

@Sirneij
Comment options

@Sirneij
Comment options

@asomers
Comment options

Answer selected by Sirneij
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants