Robert Koritnik
Robert Koritnik

Reputation: 105081

Unit tests with mock verifies

I have a unit test that

  1. creates a mock
  2. calls my method to be tested (also injecting my mock)
  3. asserts method results
  4. verifies mock calls

When mock calls don't verify as expected I get an exception, thus failing a test.
How should I correctly call this verifies? Should I just be calling

// verify property get accessor call
m.VerifyGet<bool>(p => p.IsRead, Times.AtLeastOnce());

or should I call it with Assert

// verify property get accessor call
Assert.DoesNotThrow(() => m.VerifyGet<bool>(p => p.IsRead, Times.AtLeastOnce()));

When verify fails I get an exception anyway.
What's the proper way of mock verifying?

Upvotes: 2

Views: 280

Answers (2)

Lennaert
Lennaert

Reputation: 2465

The DoesNotThrow-method should be used to test whether your own methods adhere to your specifications.

In short, adding the DoesNotThrow looks like you're testing the behaviour of VerifyGet instead of the behaviour of your SUT.

Of course, you can wrap it around the VerifyGet, but I think that only makes things confusing since VerifyGet would fail the test anyway.

Upvotes: 1

djna
djna

Reputation: 55957

VerifyGet is enough, assert seems to add no value so why add more verbiage?

Upvotes: 2

Related Questions