Moq + Unit Testing - System.Reflection.TargetParameterCountException: parameter counter mismatch

I am trying to use a lambda with a function with several parameters, but Moq throws this exception at runtime when I try to call the string mock.Object.Convert(value, null, null, null); .

  System.Reflection.TargetParameterCountException: Parameter count mismatch 

The code:

 var mock = new Mock<IValueConverter>(); mock.Setup(conv => conv.Convert(It.IsAny<Object>(), It.IsAny<Type>(), It.IsAny<Object>(), It.IsAny<CultureInfo>())).Returns((Int32 num) => num + 5); var value = 5; var expected = 10; var actual = mock.Object.Convert(value, null, null, null); 

What is the correct way to implement it?

+57
c # unit-testing moq wpf ivalueconverter
Oct 10 '11 at
source share
4 answers

This is your Returns offer. You have 4 parameters that you are setting, but you use only 1 lambda parameter. I ran the following without any problems:

 [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); } 

There are no exceptions, the test passed.

+103
Oct 10 2018-11-11T00:
source share

Not an answer for the OP, but perhaps for future googlers:

I had a Callback that did not match the signature of the method being installed

 Mock .Setup(r => r.GetNextCustomerNumber(It.IsAny<int>())) .Returns(AccountCounter++) .Callback<string, int>(badStringParam, leadingDigit => { // Doing stuff here, note that the 'GetNextCustomerNumber' signature is a single int // but the callback unreasonably expects an additional string parameter. }); 

This was the result of some refactoring, and the refactoring tool, of course, could not understand that the Callback signature was incorrect

+3
Aug 03 '16 at 0:47
source share

Perhaps this is because you are passing null , but It.IsAny<Object>() expects 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 blow in the dark from me, I am more familiar with Rhino.Mocks.




My second guess:

By looking at Moq.chm, which comes with a download,

You are using the Setup(Expression<Action<T>>) method Setup(Expression<Action<T>>) , which "Indicates the setting for the laugh type to invoke the void method."

You want to use the te Setup<TResult>(Expression<Func<T,TResult>>) method, which "Indicates the setting for the laughing type to invoke the return method."

So you can try:

 mock.Setup<Int32>( conv => { conv.Convert( It.IsAny<Object>(), It.IsAny<Type>(), It.IsAny<Object>(), It.IsAny<CultureInfo>()); return num + 5; }); 
+2
Oct 10 '11 at 15:05
source share

In my case, I thought that the type in Returns<> is an output type, but actually it was an input type.

So, if you have a method

 public virtual string Foo(int a, int b) { ... } 

The correct sentence is .Returns<int, int>(...) , NOT .Returns<string>(...) , which was originally.

My mistake was that I first tested a function with the same source and return types - for example public virtual string Foo(string a) .

+1
Nov 28 '16 at 11:33
source share



All Articles