New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support mocking foreign-defined traits #164
Comments
Mocking foreign traits and by extension directly depending on a trait defined in an external crate (excluding crates in the same workspace) seems like a clear violation of the Dependency Inversion Principle, which states that one should depend upon abstractions and not details. To quote Robert C. Martin (Clean Architecture, Frameworks are Details):
I believe this statement should also be applied to libraries in general. |
Huh? Why would it be bad to mock e.g. io::Read? Abstracting over that interface certainly wouldn't be bad practice. |
You're absolutely right, I forgot about traits defined in This was also mentioned on the Reddit thread by Matthias247:
|
Another point that was mentioned in the Reddit thread is being able to keep the "production" code free of testing annotations.
I agree with the sentiment of this, however I don't like the fact that trait definitions would need to be maintained in two separate locations. |
I'll also throw this out there -- the "components that plug into your core code" need testing as well. And in many cases there's no feasible way to do that without a mock over the external interface they're wrapping. |
I think something like serde's #[mockable(remote = "io::Write")]
trait Write {
fn write(&mut self, buf: &[u8]) -> io::Result<usize>;
fn flush(&mut self) -> io::Result<()>;
} |
Seems pretty easy-- probably just an extra annotation to indicate that the trait definition itself should be omitted from the generated code, and only the mocks should be kept.
The text was updated successfully, but these errors were encountered: