moq 中的错误“表达式树可能不包含使用可选参数的调用或调用”

Kgn*_*web 6 c# nunit moq asp.net-core

在尝试使用以下方法模拟 AWS Cognito 注册方法时 Moq C#

 public async void Signup(UserTO user)
    {

        var req = new SignUpRequest()
        {

        };
        _cognito.Setup(m =>
            m.SignUpAsync(It.IsAny<SignUpRequest>())) // LOE
            .ReturnsAsync(() =>
            new SignUpResponse()
            {

            });
    }
Run Code Online (Sandbox Code Playgroud)

但是在#LOE 处,出现以下错误

错误 CS0854 表达式树可能不包含使用可选参数的调用或调用

如果我按 f12 来获取 SignUpAsync() 的定义,它看起来像

Task<SignUpResponse> SignUpAsync(SignUpRequest request, CancellationToken cancellationToken = default(CancellationToken));
Run Code Online (Sandbox Code Playgroud)

是什么导致了这个错误以及如何摆脱这个错误?

谢谢!

Nko*_*osi 5

模拟期望配置/设置整个成员定义

期望选项参数使用 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());

    //...
}
Run Code Online (Sandbox Code Playgroud)