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

Question: would it make sense to call verify all on a strict mock when the mock gets disposed? #1440

Open
basprins opened this issue Nov 8, 2023 · 0 comments

Comments

@basprins
Copy link

basprins commented Nov 8, 2023

To start with explaining my own wrong expectation: I thought that a strict mock would implicitly verify all when it was disposed. This is somewhat a stupid assumption, since IMock doesn't implement IDisposable at all... So that was quite the revelation when I removed a call on an interface and expected the unit test would fail, which injected a strict mock for that dependency. The test passes all the same, until I explicitly call into mock.VerifyAll()

We use moq for years, and recently started out with autofac and thus Autofac.Extras.Moq to unleash the power of AutoMock. I am a fan from the moment we started out with this.

The mock repository returned by AutoMock.GetStrict() gives me an instance which is disposable. That's where I made the wrong assumption, and assumed that disposing the instance return by AutoMock.GetStrict() would also dispose all the mocks registered in the autofac container, and consequently it would call VerifyAll on these mocks. Now I know, this is not the case. But I was wondering, could you? Would it make sense to have such a feature? Especially for strict mocks I would really like the implicit check if all expectations were satisfied. Rather that, then having to add the boilerplate in every unit test to call VerifyAll on all registered mocks...

Even without AutoMock I would be able to declare a mock as follows

using var mock = new Mock<ISomeDependency>();

And when the mock runs out of scope, it would call VerifyAll in the dispose and I would be notified if I had expectations which weren't met.

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

1 participant