C# 使用C Moq测试获得参数计数不匹配?
我知道也有类似的问题,但不知何故,我无法了解我的情况。我收到参数计数不匹配异常 下面是我如何注册我的模拟C# 使用C Moq测试获得参数计数不匹配?,c#,.net,wpf,unit-testing,moq,C#,.net,Wpf,Unit Testing,Moq,我知道也有类似的问题,但不知何故,我无法了解我的情况。我收到参数计数不匹配异常 下面是我如何注册我的模拟 var couponService = DependencyResolver.Resolve<Mock<ICouponWebServiceAdapter>>(); couponService.Setup(a => a.checkCouponAvailability(It.IsAny<orderLine[]&g
var couponService =
DependencyResolver.Resolve<Mock<ICouponWebServiceAdapter>>();
couponService.Setup(a =>
a.checkCouponAvailability(It.IsAny<orderLine[]>(),
It.IsAny<orderHeader>()))
.Returns((couponDetail[] request) =>
{
var coupon = new couponDetail
{
description = "75% off the original price",
value = 50
};
var coupon1 = new couponDetail
{
description = "500 off the original price",
value = 20
};
var coupondetails = new couponDetail[] { coupon, coupon1 };
return coupondetails;
});
checkCouponAvailability正在返回couponDetail[]
我做错了什么?我尝试将我的return设置为IQueryable似乎在Returns方法内部指定了一个名为request的参数,该参数的类型为couponDetail[],但该方法本身采用orderLine[],orderHeader的参数。传入的方法将使用传入模拟方法的实际参数调用,这将导致您得到的ParameterCountMismatchException 通过在模拟函数之前模拟返回,可以传入所需的文本对象。示例如下: 您可以将一个方法传递给returns,它必须接受传递到原始方法中的所有参数。示例如下:
您可以发布checkCouponAvailability的方法签名吗?如果对代码没有更多的了解,在这里很难提供帮助。你需要提供一个解决方案。就像5分钟前一样,这是第一个选择。非常感谢你的帮助!
var coupondetails = new couponDetail[] {
new couponDetail
{
description = "75% off the original price",
value = 50
},
new couponDetail
{
description = "500 off the original price",
value = 20
}
};
var couponService = DependencyResolver.Resolve<Mock<ICouponWebServiceAdapter>>();
couponService
.Setup(a => a.checkCouponAvailability(It.IsAny<orderLine[]>(), It.IsAny<orderHeader>()))
.Returns(coupondetails);
var couponService = DependencyResolver.Resolve<Mock<ICouponWebServiceAdapter>>();
couponService
.Setup(a => a.checkCouponAvailability(It.IsAny<orderLine[]>(), It.IsAny<orderHeader>()))
.Returns((orderLine[] arg1, orderHeader arg2) => {
return new couponDetail[] {
new couponDetail
{
description = "75% off the original price",
value = 50
},
new couponDetail
{
description = "500 off the original price",
value = 20
}
};
});