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

标签 c# asp.net-core nunit moq

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

相关文章:

.net - 使用/clr :safe to enable unit testing? 编译 C++ 项目有什么缺点

c# - 最快/最安全的文件查找/解析?

.net - DNU 从 MSBuild 发布 --no-source

c# - "System.Net.Sockets.Socket does not contain a definition for ' 发送至 '"

c# - 如何在我的 .NET Core API 中使用 Windows 环境变量?

c# - 从 HttpResponseMessage 获取 Excel 文件

c# - 为什么启用 JetBrains dotCover 会导致 NUnit 测试失败?

c# - 如何使用 nunit 测试异步方法

c# - 重构 C# 国际象棋游戏以遵循 MVC 设计模式

c# - 复制构造函数转到基础构造函数并覆盖复制的值