Moq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch

后端 未结 4 369
轻奢々
轻奢々 2020-11-30 12:15

I\'m tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, n

4条回答
  •  抹茶落季
    2020-11-30 12:36

    Perhaps it's because you are passing null but It.IsAny() is expecting any object except null? What happens if you do the following?:

    var actual = mock.Object.Convert(value, new object(), typeof(object), CultureInfo.CurrentCulture);
    

    This is just a stab in the dark from me, I'm more familiar with Rhino.Mocks.


    My 2nd guess:

    Having looked at the Moq.chm that comes with the download,

    You are using the Setup(Expression>) method which "Specifies a setup on the mocked type for a call to a void method."

    You want te Setup(Expression>) method that "Specifies a setup on the mocked type for a call to a value returning method".

    So you could try:

    mock.Setup(
        conv => {
            conv.Convert(
                It.IsAny(), 
                It.IsAny(),
                It.IsAny(), 
                It.IsAny());
            return  num + 5;
            });
    
        

    提交回复
    热议问题