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

Thorough aggregation of verification errors & more complete error messages #762

Merged
merged 4 commits into from
Feb 27, 2019

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Feb 27, 2019

Verification using mock.Verify[All] has always been performed in a "recursive" manner (i.e. inner mocks were verified along with the outer / owning mock). However, in the case of failure, verification would often stop and only describe the first error in the message.

This PR makes verification more thorough by aggregating verification errors at all levels:

  • Mock repositories aggregate errors of all their mocks. (This was done already.)
  • Mocks aggregate errors of all setups. (This is new.)
  • Setups can forward errors from an inner mock. (This is also new.)

For example, given this code:

var mock = new Mock<IOuter>();
mock.Setup(m => m.Inner.Method());
mock.Setup(m => m.AnotherMethod());
mock.VerifyAll();

you might now see such error message:

Mock<IOuter:00000001>:
This mock failed verification due to the following:

   IOuter mock => mock.Inner:

      Mock<IInner:00000001>:
      This mock failed verification due to the following:

         IOuter m => m.Inner.Method():
         This setup was not matched.

   IOuter m => m.AnotherMethod():
   This setup was not matched.

instead of the previous:

The following setups on mock 'Mock<IOuter:00000001>' were not matched:
IOuter mock => mock.Inner
IOuter m => m.AnotherMethod()

The trade-off here is obviously brevity vs. completeness. The latter probably makes more sense in an error message (IMO).

@stakx stakx added this to the 4.11.0 milestone Feb 27, 2019
...and aggregate them properly. This changes the formatting of verifi-
cation error messages to use indentation to represent the hierarchical
relationship between mocks, setups, and inner mocks.
...by delegating inner mock verification to inner mock setups. These
can then augment their inner mock's error message so it becomes appa-
rent through which calls one would arrive at the failed inner mock.

After this commit, this code:

   var mock = new Mock<IInner>;
   mock.Setup(m => m.Inner.Method());
   mock.VerifyAll();

might produce this error message:

   Mock<IOuter:00000001>:
   This mock failed verification due to the following:

      IOuter m => m.Inner:            <-- added by this commit!

         Mock<IInner:00000001>:
         This mock failed verification due to the following:

            IInner m => m.Inner.Method():
            This setup was not matched.
@stakx stakx merged commit 31bd735 into devlooped:master Feb 27, 2019
@stakx stakx deleted the recursive-verification branch February 27, 2019 21:39
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