Skip to content
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

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

Closed
jaques-sam opened this issue May 17, 2024 · 4 comments
Closed

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

jaques-sam opened this issue May 17, 2024 · 4 comments

Comments

@jaques-sam
Copy link

jaques-sam commented May 17, 2024

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
Copy link
Owner

asomers commented May 17, 2024

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
Copy link
Author

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:

  • a parameter added to new()
  • another initialization function
  • a new no_expectation() method on the Mock (<-- preferred)

@asomers
Copy link
Owner

asomers commented May 21, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants