I have a class called AuthManager. It implements an interface IAuthManager. The interface has two methods. AuthorizeTransaction() and CancelTransaction()
Let say I call AuthorizeTransaction() and it throws an exception, in the catch block I handle exception and call CancelTransaction().
How can I verify with Moq that my CancelTransaction() was called. I tried this with Mock mock = new Mock(). Then mock.Verify(…) to verify CancelTransaction() method was called. But I get error Non-override method cannot be verified. I don’t want to put virtual keyword on the method just so the test can pass. Is there anyway to verify CancelTransaction() was called upon exception? Thank you.
Update: I followed the link provided by Michael. This is my code below. But my testing is failing because of this error Expected invocation on the mock once, but was 0 times
// Arrange
mockAuth = new Mock<IAuthManager>();
mockAuth.Setup(mock => mock.CancelAuthorization(It.IsAny<CancelAuthorizationModel>()));
var auth = new AuthManager(new FakeDataRepo(), fakeDataManager, fakeCardManager(), fakeSearchManager, fakeTokenManager, new fakeConnection(), fakeDataManager);
var authModel = GenerateModel(
type: ...........,
transactionType: ...........,
transactionNumber: .........);
try
{
// Act
await auth.AuthorizeTransaction(authModel);
}
catch (Exception ex)
{
// Assert
mockAuth.Verify(x => x.CancelTransaction(It.IsAny<CancelTransactionModel>()), Times.Once);
}