-1

I am using Moq for my unit tests. I have this interface:

public interface IMyInterface
{
    Task<AClass> MyMethod(int arg1, string arg2=0, int arg3=1, bool arg4=false);
}

In my unit test code, I have

var mockInterface = new Mock<IMyInterface>();
mockInterface.Setup(w => w.MyMethod(It.IsAny<int>(), It.IsAny<string>(),
                                    It.IsAny<int>(), It.IsAny<bool>()))

My unit test code compiles, but when I run it, I get an exception saying

System.AggregateException: One or more errors occurred. ---> System.Reflection.TargetParameterCountException: Parameter count mismatch.
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Moq.Extensions.InvokePreserveStack(Delegate del, Object[] args)
   at Moq.MethodCallReturn`2.Execute(ICallContext call)
   at Moq.Interceptor.Intercept(ICallContext invocation)

I have checked that the argument count does match, but i am not sure why I am getting this exception.

Thank you.

n179911
  • 19,547
  • 46
  • 120
  • 162
  • Apart from the fact that arg2's default should be a string ("" or null) and not (0). I am unable to replicate your problem as everything else seems to be as it should. please provide a [mcve] that reproduces the problem so that better answers can be provided. – Nkosi Jan 07 '17 at 02:03

1 Answers1

1

You didn't specify a return statement to your setup.

Change your code to:

mockInterface.Setup(w => w.MyMethod(It.IsAny<int>(), It.IsAny<string>(), 
                                    It.IsAny<int>(), It.IsAny<bool>()))
             .ReturnsAsync(new AClass());

This will solve your problem

Old Fox
  • 8,629
  • 4
  • 34
  • 52