vendredi 2 avril 2021

What is an idiomatic way to implement a trait only for tests across multiple crates?

I work in a workspace with dozens of crates. One of those crates exposes a trait. As a mock, I implement that trait for () with unimplemented! in every function (they're not actually used). I'd like that implementation to be available from the other crates, but only during the tests: what is the idiomatic way (the handiest) to do so?

For now, the implementation is behind a mock feature, and I add this crate with the mock feature as a dev dependency in a random crate. That forces the compiler to take that implementation into account during the tests. It's an ugly hack, so I'd rather have another way.

Aucun commentaire:

Enregistrer un commentaire