Skip to content

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

@basprins

Description

@basprins

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.

Back this issue
Back this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions