简体   繁体   中英

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

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 (ICustomerRequest 1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest 1,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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM