使用 C# Moq 测试获取参数计数不匹配?
Using C# Moq testing getting Parameter count mismatch?
我知道有类似的问题,但不知何故我无法弄清楚我的情况。我收到参数计数不匹配异常。
这是我注册 Mock 的方式,
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 是 returning couponDetail[]
我做错了什么?我试着把我的 return 作为 IQueryable
您似乎在 Returns
方法内部指定了一个类型为 couponDetail[]
的名为 request
的参数,但该方法本身采用 (orderLine[], orderHeader)
的参数。传递给 Returns
的方法将使用传递给模拟方法的实际参数调用,这将导致您得到 ParameterCountMismatchException。
- 您可以在模拟函数之前通过模拟 return 来传入所需的文字对象。下面的示例:
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);
- 您可以将方法传递给 returns,该方法必须采用传递给原始方法的所有参数。下面的示例:
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
}
};
});
我知道有类似的问题,但不知何故我无法弄清楚我的情况。我收到参数计数不匹配异常。
这是我注册 Mock 的方式,
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 是 returning couponDetail[]
我做错了什么?我试着把我的 return 作为 IQueryable
您似乎在 Returns
方法内部指定了一个类型为 couponDetail[]
的名为 request
的参数,但该方法本身采用 (orderLine[], orderHeader)
的参数。传递给 Returns
的方法将使用传递给模拟方法的实际参数调用,这将导致您得到 ParameterCountMismatchException。
- 您可以在模拟函数之前通过模拟 return 来传入所需的文字对象。下面的示例:
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);
- 您可以将方法传递给 returns,该方法必须采用传递给原始方法的所有参数。下面的示例:
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
}
};
});