Moq & C#: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

混江龙づ霸主 提交于 2019-12-04 03:52:08

问题


The actual interface signature goes like this

Task<GeneralResponseType> UpdateAsync(ICustomerRequest<IEnumerable<CustomerPreference>> request, CancellationToken cancellationToken, ILoggingContext loggingContext = null);

Testcase:

ICustomerRequest<IEnumerable<CustomerPreference>> t = null;
CancellationToken t1 = new CancellationToken();
LoggingContext t2 = null;
this.customerPreferenceRepositoryMock.Setup(x => x.UpdateAsync(
        It.IsAny<ICustomerRequest<IEnumerable<CustomerPreference>>>(),
        It.IsAny<CancellationToken>(),
        It.IsAny<LoggingContext>()))
    .Callback<ICustomerRequest<IEnumerable<CustomerPreference>>,CancellationToken, LoggingContext>((a, b, c) => { t = a ; t1 =b;t2= c; });

The setup is throwing an exception in the testcase,as below

Invalid callback. Setup on method with parameters (ICustomerRequest1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest1,CancellationToken,LoggingContext).

What is wrong I am doing?

I have verified Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

But I didn't see any help.


回答1:


As mentioned in the comments the Callback parameters used do not match the method definition. Even though the Setup uses It.IsAny<LoggingContext> the method definition uses ILoggingContext parameter

Change t2 to

ILoggingContext t2 = null;

And update the Callback to

.Callback<ICustomerRequest<IEnumerable<CustomerPreference>>,CancellationToken, ILoggingContext>((a, b, c) => { 
    t = a; 
    t1 = b;
    t2 = c; 
});

or

.Callback((ICustomerRequest<IEnumerable<CustomerPreference>> a, 
           CancellationToken b, 
           ILoggingContext c) => { 
        t = a; 
        t1 = b;
        t2 = c; 
    });

Either way will work.

I would also advise that the Setup return a completed Task so as to allow for the test to flow asynchronously as expected.

this.customerPreferenceRepositoryMock
    .Setup(x => x.UpdateAsync(
        It.IsAny<ICustomerRequest<IEnumerable<CustomerPreference>>>(),
        It.IsAny<CancellationToken>(),
        It.IsAny<LoggingContext>()))
    .Callback((ICustomerRequest<IEnumerable<CustomerPreference>> a, 
               CancellationToken b, 
               ILoggingContext c) => { 
                    t = a; 
                    t1 = b;
                    t2 = c; 
                    //Use the input to create a response
                    //and pass it to the `ReturnsAsync` method
             })
    .ReturnsAsync(new GeneralResponseType()); //Or some pre initialized derivative.

Review Moq's QuickStart to get a better understanding of how to use the framework.



来源:https://stackoverflow.com/questions/44000631/moq-c-invalid-callback-setup-on-method-with-parameters-cannot-invoke-callba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!