asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.45k stars 61 forks source link

How to create a "dummy" mock where no expectations are required #578

Closed jaques-sam closed 3 months ago

jaques-sam commented 3 months ago

I couldn't find my answer anywhere. I remember from the Google Test framework I could create mocks that don't require any expectation to be set so tests would pass (see https://google.github.io/googletest/reference/mocking.html#NiceMock where you also have NaggyMock & StrictMock).

The current scenario I have is, I can call 2 dependencies where I want 2 separate tests where I check the expectations for each one of them.

asomers commented 3 months ago

You can already do this, partially. If your methods return type implements Default and you build mockall with the "nightly" feature, then every expectation will automatically return its default value. But you still must create the expectation.

jaques-sam commented 3 months ago

Thanks for your reply! I'll have a try. I don't feel this is the right way to go, can this issue be morphed into a feature request? I could also be a simple function with either:

asomers commented 3 months ago

No, it really can't become a new feature. At least, not on stable Rust, because absent Specialization there's no way for Mockall to know what return value to use if the user doesn't supply one. Also, I'm philosophically opposed to allowing methods to be called without explicit configuration from the user. PyMock allows that, and in my experience it's caused far more problems than it's solved.

jaques-sam commented 3 months ago

Okay, I like KISS too. At least we discussed it so people can find it back.

Maybe I have to revise my implementation I need to test or my test itself. What I currently did as a workaround was creating my own "dummy" mock with simply empty implementation of the trait, which wasn't so bad.