Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in moq "An expression tree may not contain a call or invocation that uses optional arguments"

While trying to MOCK AWS Cognito Signup Method using Moq C#

 public async void Signup(UserTO user)
    {

        var req = new SignUpRequest()
        {

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

            });
    }

But at the #LOE, getting the below error

Error CS0854 An expression tree may not contain a call or invocation that uses optional arguments

If I press f12 to get the definition of the SignUpAsync(), it looks as

Task<SignUpResponse> SignUpAsync(SignUpRequest request, CancellationToken cancellationToken = default(CancellationToken));

What's causing this error & how to get rid of this?

Thanks!

like image 954
Kgn-web Avatar asked Sep 09 '19 11:09

Kgn-web


1 Answers

The mock expects the entire member definition to be configured/setup

Expect the options parameter using 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());

    //...
}
like image 147
Nkosi Avatar answered Sep 26 '22 01:09

Nkosi