Moq如何确定使用包含某些值的列表来调用方法 [英] Moq how determine a method was called with a list containing certain values

查看:96
本文介绍了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>>());

所以这可以很好地工作,并检查是否调用了GeneratePaymentStatus,但只有在使用任何旧的int列表时才调用它.

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

有没有一种方法可以重写它,以便测试是否用包含2和3的整数列表调用了GeneratePaymentStatus?

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

推荐答案

类似的东西:

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

这篇关于Moq如何确定使用包含某些值的列表来调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆