'Using C# Moq testing getting Parameter count mismatch?
I know there are similar questions but somehow I am not able to figure out the situation in my case. I am getting Paramater count mismatch exception.
Here is how I am registering my 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;
});
the checkCouponAvailability is returning couponDetail[]
What am I doing wrong? I tried putting my return as IQueryable
Solution 1:[1]
It appears that inside of the Returns
method you are specifying a parameter called request
of type couponDetail[]
, yet the method itself takes the parameters of (orderLine[], orderHeader)
. The method that is passed into Returns
gets invoked with the actual parameters that are passed into your mocked method which will cause the ParameterCountMismatchException you are getting.
- You can pass in the literal object you want by mocking your return before mocking the function. Example below:
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);
- You can pass a method to returns which MUST take all of the arguments passed into the original method. Example below:
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
}
};
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |