[英]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).具有参数(ICustomerRequest
1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest
1,CancellationToken,LoggingContext)的1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest
。
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. 如注释中所述,所使用的
Callback
参数与方法定义不匹配。 Even though the Setup
uses It.IsAny<LoggingContext>
the method definition uses ILoggingContext
parameter 即使
Setup
使用It.IsAny<LoggingContext>
,方法定义也使用ILoggingContext
参数
Change t2
to 将
t2
更改为
ILoggingContext t2 = null;
And update the Callback
to 并将
Callback
更新为
.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. 我也建议
Setup
返回一个完成的Task
,以便允许测试按预期异步进行。
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. 查看Moq的快速入门 ,以更好地了解如何使用该框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.