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

It’s your Returns clause. You have a 4 parameter method that you’re setting up, but you’re only using a 1 parameter lambda. I ran the following without issue:

[TestMethod]
public void IValueConverter()
{
    var myStub = new Mock<IValueConverter>();
    myStub.Setup(conv => conv.Convert(It.IsAny<object>(), It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<CultureInfo>())).
        Returns((object one, Type two, object three, CultureInfo four) => (int)one + 5);

    var value = 5;
    var expected = 10;

    var actual = myStub.Object.Convert(value, null, null, null);

    Assert.AreEqual<int>(expected, (int) actual);
}

No exceptions, test passed.

Leave a Comment