Reputation: 3509
Is it possible to verify a method call in an Assert.Multiple
block alongside other calls to Assert
?
My current solution does not call the assert on SomeProperty
, when the method call to MyMethod
does not verify.
And the only way to get close to what I want would be to move the call to myInterfaceMock.Verify
to the end, but that does no longer work when there are multiple method calls to verify.
var mInterfaceMock = new Mock<IMyInterface>()
.Setup(x => x.MyMethod())
.Verifiable();
var systemUnderTest = new MyClass(myInterfaceMock.Object);
systemUnderTest.MethodToTest();
Assert.Multiple(() => {
myInterfaceMock.Verify();
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
Upvotes: 6
Views: 1218
Reputation: 247561
The verify will throw its own exception that the assertion block wont know how to handle. That is why when the verify fails nothing gets invoked after it.
The notes in documentation also states
The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is often an indication that the test itself is in error, so it must be terminated. If the exception occurs after one or more assertion failures have been recorded, those failures will be reported along with the terminating exception itself.
Reference Multiple Asserts
Consider the following approach
//...
Assert.Multiple(() => {
Assert.That(() => myInterfaceMock.Verify(), Throws.Nothing);
Assert.That(systemUnderTest.SomeProperty, Is.True);
});
If the verify throws an exception then it will be handled by its own assertion.
Upvotes: 9
Reputation: 317
If your MethodToTest()
does not return anything they I'd recommend just verifying if the method run, as you're trying to do with the assert, but I'd recommend doing something like this:
mInterfaceMock.Verify(x => x.MethodToTest(), Times.Once);
just specifying the times it has had to run, without it inside of an assertion
Upvotes: 0