Programing

Moq + 단위 테스트-System.Reflection.TargetParameterCountException : 매개 변수 개수 불일치

lottogame 2020. 11. 11. 07:50
반응형

Moq + 단위 테스트-System.Reflection.TargetParameterCountException : 매개 변수 개수 불일치


다중 매개 변수 함수와 함께 람다를 사용하려고하지만 Moq는 mock.Object.Convert(value, null, null, null);을 호출하려고 할 때 런타임에이 예외를 throw합니다 .

System.Reflection.TargetParameterCountException : 매개 변수 개수 불일치

코드는 다음과 같습니다.

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

그것을 구현하는 적절한 방법은 무엇입니까?


귀하의 Returns조항입니다. 설정중인 4 개의 매개 변수 메서드가 있지만 1 개의 매개 변수 람다 만 사용하고 있습니다. 나는 문제없이 다음을 실행했습니다.

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

예외는 없으며 테스트를 통과했습니다.


OP에 대한 답변이 아니라 미래의 Google 직원을위한 답변 :

나는 한 Callback방법의 존재 설정의 서명과 일치하지 않았다을

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

이것은 일부 리팩토링의 결과였으며 리팩토링 도구는 물론 Callback서명이 잘못 되었음을 인식 할 수 없었습니다.


아마도 당신이 지나가고 null있지만 It.IsAny<Object>()어떤 object것을 기대하고 있기 때문일 것입니다 null. 다음을 수행하면 어떻게됩니까? :

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

이것은 저에게 어둠 속에서 찌르는 것입니다. 저는 Rhino에 더 익숙합니다.


내 두 번째 추측 :

다운로드와 함께 제공되는 Moq.chm을 살펴보면

당신이 사용하는 Setup(Expression<Action<T>>)방법을하는 "지정에 대한 호출에 대한 조롱 유형의 설정 void방법."

Setup<TResult>(Expression<Func<T,TResult>>)"값 반환 메서드에 대한 호출을 위해 모의 형식에 대한 설정을 지정"하는 메서드를 원합니다 .

따라서 시도해 볼 수 있습니다.

mock.Setup<Int32>(
    conv => {
        conv.Convert(
            It.IsAny<Object>(), 
            It.IsAny<Type>(),
            It.IsAny<Object>(), 
            It.IsAny<CultureInfo>());
        return  num + 5;
        });

In my case, I thought that the type in Returns<> is the output type, but in fact it was the input type(s).

So if you have a method

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

The correct clause is .Returns<int, int>(...), NOT .Returns<string>(...) which is what I thought initially.

My mistake was because I was testing a function with the same input and return type initially - for example public virtual string Foo(string a).

참고URL : https://stackoverflow.com/questions/7714551/moq-unit-testing-system-reflection-targetparametercountexception-parameter

반응형