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

Expose Mock.Setups, part 3: Individual setup verification #987

Merged
merged 9 commits into from
Apr 1, 2020

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Mar 31, 2020

(This is a follow-up to #985.)

This adds void Verify(bool recursive = true) and void VerifyAll() methods to ISetup.

@stakx stakx added this to the 4.14.0 milestone Mar 31, 2020
@stakx stakx changed the title Expose Mock.Setups, part 3: Individual setup verification Expose Mock.Setups, part 3: Individual setup verification (WIP) Mar 31, 2020
src/Moq/ISetup.cs Outdated Show resolved Hide resolved
src/Moq/ISetup.cs Outdated Show resolved Hide resolved
src/Moq/Setup.cs Outdated Show resolved Hide resolved
Comment on lines +194 to +197
foreach (Invocation invocation in this.mock.MutableInvocations)
{
invocation.MarkAsVerifiedIfMatchedBy(setup => setup == this);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this only happen if verification actually succeeds? Same question goes for the corresponding loop in Mock.Verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the current logic isn't perfect, it's generally good enough: if verification fails, an exception gets thrown anyway; it's unlikely someone catches that exception (Moq documentation explicitly recommends not doing that!) and then goes on to inspect the mock's state.

There may possibly be some odd corner cases regarding the verification status of invocations on a setup's inner mock; let's deal with those when they become an actual problem.

tests/Moq.Tests/SetupFixture.cs Outdated Show resolved Hide resolved
Despite the same name, they differ from the `Verify[All]` methods of
`Mock` mainly in one aspect: The setup on which the method is invoked
will always be verified, regardless of whether or not it is flagged
verifiable.

That is, `setup.Verify()` will verify `setup` even if it has not been
marked `.Verifiable()`. It wouldn't make sense for `setup.Verify()` to
be a no-op; otherwise, why would one make the call in the first place?

These new method signatures have the added benefit of allowing us to
later introduce (both on `ISetup` and `Mock`) an additional overload:

    void Verify(bool recursive, Func<ISetup, bool> predicate)

which will allow custom setup selection without conflicting with any
existing `Verify` methods.
The exact location where this `foreach` loop gets added is important:

The simplest solution with the fewest lines of code would be to mark
matching invocations inside `Setup.TryVerify`, and to remove the corr-
esponding loop from `Mock.TryVerify`.

However, since `Setup.TryVerify` is called by `Mock.TryVerify` for each
setup, that would mean iterating over invocations once per setup. It
is much more efficient to iterate once per mock in `Setup.TryVerify`.

This is why we are adding the new `foreach` loop in `Setup.Verify`, so
it only executes when we enter verification at this setup (and so "by-
passed" the batch logic in `Mock`).
@stakx stakx changed the title Expose Mock.Setups, part 3: Individual setup verification (WIP) Expose Mock.Setups, part 3: Individual setup verification Apr 1, 2020
@stakx stakx merged commit b44b150 into devlooped:master Apr 1, 2020
@stakx stakx deleted the mock-setups-3 branch April 1, 2020 22:10
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

Successfully merging this pull request may close these issues.

None yet

1 participant