Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Moq to verify a CommandHandler method call with correct Command Parameters

I have the following test case:

    [Test]
    public void MarkAsSuccessfulTest()
    {
        //setup data
        var payment = Util.DbUtil.CreateNewRecurringProfilePayment();

        //unit test

        var mockNotificationSender = new Mock<IMarkAsSuccessfulNotificationSender>();
        var mockCommandHandler = new Mock<IDbCommandHandler<RecurringPaymentMarkAsSuccessfulCommand>>();

        var classUnderTest = new RecurringProfileMarkLastPaymentAsSuccessful(mockCommandHandler.Object, mockNotificationSender.Object);

        classUnderTest.MarkAsSuccessful(payment.RecurringProfile);
        mockCommandHandler.Verify(x=>x.Handle(It.IsAny<RecurringPaymentMarkAsSuccessfulCommand>()), Times.Once());
        mockNotificationSender.Verify(x=>x.SendNotification(payment), Times.Once());

    }

The issue is with the line:

mockCommandHandler.Verify(x=>x.Handle(It.IsAny<RecurringPaymentMarkAsSuccessfulCommand>()), Times.Once())

This verifies that the .Handle() method was called. However, this is not enough for the test - This .Handle() takes a command parameter, which has one property - Payment. I would like to verify that this parameter was actually matching the payment variable.

Is this possible, or is there an issue with some of the code-design?

like image 413
Karl Cassar Avatar asked Dec 06 '25 09:12

Karl Cassar


1 Answers

You can provide predicate for parameter verification:

mockCommandHandler.Verify(x => 
   x.Handle(It.Is<RecurringPaymentMarkAsSuccessfulCommand>(c => c.Payment == payment))
   , Times.Once());
like image 129
Sergey Berezovskiy Avatar answered Dec 07 '25 21:12

Sergey Berezovskiy