最小起订量C#:无效的回调.具有参数的方法上的设置无法调用具有参数的回调 [英] Moq & C#: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

查看:82
本文介绍了最小起订量C#:无效的回调.具有参数的方法上的设置无法调用具有参数的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际的接口签名是这样的

The actual interface signature goes like this

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

测试用例:

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

无效的回调.使用参数设置方法 (ICustomerRequest 1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest 1,CancellationToken,LoggingContext).

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

我在做什么错?

我已验证 Moq:无效的回调.具有参数的方法上的设置无法调用具有参数的回调

但是我没有看到任何帮助.

But I didn't see any help.

推荐答案

如注释中所述,所使用的Callback参数与方法定义不匹配.即使Setup使用It.IsAny<LoggingContext>,方法定义也使用ILoggingContext参数

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

t2更改为

ILoggingContext t2 = null;

并将Callback更新为

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

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

这两种方法都可以.

我还建议Setup返回完整的Task,以便允许测试按预期方式异步进行.

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.

查看Moq的快速入门,以更好地了解如何使用该框架.

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

这篇关于最小起订量C#:无效的回调.具有参数的方法上的设置无法调用具有参数的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆