Moq how to define a method called with a list containing specific values

Hi, I have a method with the following signature:

public void GeneratePaymentAdvise(IList<int> paymentIds) 

and this is called in another way:

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

So, in unit test, I want to make sure that it is 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 will work fine and checks that GeneratePaymentStatus is being called, but only the one that was called with any old list of ints.

Is there a way to rewrite this so that it tests that GeneratePaymentStatus has been called with an int list containing 2 and 3?

+6
source share
2 answers

Something like that:

 mockPaymentLogic.Verify(x => x.GeneratePaymentStatus(It.Is<IList<int>>(l => l.Contains(2) && l.Contains(3)))); 
+7
source

Why don't you just use the same int array for verification? Something like that..

 [TestClass] public class SomeClassTests { [TestMethod] public void UpdatePaymentStatus_PaymentIds_VerifyPaymentLogicGeneratePaymentStatus() { var mock = new Mock<IPaymentLogic>(); var sut = new Sut(); var idList = new List<int> {2, 3}; sut.UpdatePaymentStatus(idList, mock.Object); mock.Verify(x => x.GeneratePaymentStatus(idList)); } } public class Sut { public void UpdatePaymentStatus(IList<int> paymentIds, IPaymentLogic paymentLogic) { paymentLogic.GeneratePaymentStatus(paymentIds); } } public interface IPaymentLogic { void GeneratePaymentStatus(IList<int> paymentIds); } 

If you check against a list other than idList, the test will fail.

0
source

Source: https://habr.com/ru/post/947449/


All Articles