gpt4 book ai didi

c# - 最小起订量错误 "An expression tree may not contain a call or invocation that uses optional arguments"

转载 作者:行者123 更新时间:2023-11-30 18:11:32 25 4
gpt4 key购买 nike

尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com