最小起订量和 C#:回调无效。带参数的方法设置无法调用带参数的回调
Moq & C#: Invalid callback. Setup on method with parameters cannot invoke callback with parameters
实际的接口签名是这样的
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; });
设置在测试用例中抛出异常,如下所示
Invalid callback. Setup on method with parameters
(ICustomerRequest1,CancellationToken,ILoggingContext) cannot invoke
callback with parameters
(ICustomerRequest
1,CancellationToken,LoggingContext).
我做错了什么?
我已经验证Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters
但我没有看到任何帮助。
如评论中所述,使用的 Callback
参数与方法定义不匹配。尽管 Setup
使用 It.IsAny<LoggingContext>
方法定义使用 ILoggingContext
参数
将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
return 完成 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.
查看起订量 QuickStart 以更好地了解如何使用该框架。
实际的接口签名是这样的
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; });
设置在测试用例中抛出异常,如下所示
Invalid callback. Setup on method with parameters (ICustomerRequest
1,CancellationToken,ILoggingContext) cannot invoke callback with parameters (ICustomerRequest
1,CancellationToken,LoggingContext).
我做错了什么?
我已经验证Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters
但我没有看到任何帮助。
如评论中所述,使用的 Callback
参数与方法定义不匹配。尽管 Setup
使用 It.IsAny<LoggingContext>
方法定义使用 ILoggingContext
参数
将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
return 完成 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.
查看起订量 QuickStart 以更好地了解如何使用该框架。