尝试使用 Moq
C#
模拟 AWS Cognito 注册方法时
public async void Signup(UserTO user)
{
var req = new SignUpRequest()
{
};
_cognito.Setup(m =>
m.SignUpAsync(It.IsAny<SignUpRequest>())) // LOE
.ReturnsAsync(() =>
new SignUpResponse()
{
});
}
但是在#LOE,出现以下错误
Error CS0854 An expression tree may not contain a call or invocation that uses optional arguments
如果我按 f12 获取 SignUpAsync() 的定义,它看起来像
Task<SignUpResponse> SignUpAsync(SignUpRequest request, CancellationToken cancellationToken = default(CancellationToken));
是什么原因导致此错误以及如何消除此错误?
谢谢!
最佳答案
模拟需要配置/设置整个成员定义
期望选项参数使用It.IsAny<CancellationToken>()
public async Task Signup(UserTO user) {
var req = new SignUpRequest() {
};
_cognito.Setup(m =>
m.SignUpAsync(It.IsAny<SignUpRequest>(), It.IsAny<CancellationToken>())
)
.ReturnsAsync(() => new SignUpResponse());
//...
}
关于c# - 最小起订量错误 "An expression tree may not contain a call or invocation that uses optional arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57852709/