Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq how determine a method was called with a list containing certain values [duplicate]

Tags:

c#

moq

Hi say I have a method with the following signature:

public void GeneratePaymentAdvise(IList<int> paymentIds)

and this is called by another method:

public void UpdatePaymentStatus(IList<int> paymentIds, IPaymentLogic paymentLogic)
{
 ...
   paymentLogic.GeneratePaymentStatus(paymentIds);
 ...
}

So in a unit test I want to make sure this was called. Using moq:

var mockPaymentLogic = new Mock<PaymentLogic>();

UpdatePaymentStatus(new List<int> { 2, 3 }, mockPaymentLogic.Object);

mockPaymentLogic.Verify(x => x.GeneratePaymentStatus(It.IsAny<IList<int>>());

So this would work fine and checks that GeneratePaymentStatus is called but only that was called with any old list of ints.

Is there a way to rewrite this so it tests that GeneratePaymentStatus was called with a list of ints containing 2 and 3?

like image 964
user2005657 Avatar asked Jun 17 '13 09:06

user2005657


1 Answers

Something like that:

mockPaymentLogic.Verify(x => x.GeneratePaymentStatus(It.Is<IList<int>>(l => l.Contains(2) && l.Contains(3))));
like image 186
tdragon Avatar answered Nov 01 '22 05:11

tdragon