Micah Armantrout
Micah Armantrout

Reputation: 6971

moq method call not being verified

I have a test method that is failing on sso.verify notice the CheckUsername method has two await calls in a async method ? but because of that the sso verify never returns and thus is not verified. But the code is called. What would be the proper way to test this ?

       public void Setup()
    {
        nav = new Mock<INavService>();
        sso = new Mock<ISSOApiService>();
        _vm_Successs = new ForgotPasswordViewModel(nav.Object, sso.Object);
        sso.Setup(x => x.SendEmailCodeRequestAsync(It.IsAny<PasswordTokenRequest>())).ReturnsAsync(new StandardResponse() { ErrorCode = null }).Verifiable();
        nav.Setup(x => x.NavigateTo<ForgotPasswordEnterCodeModel, string>(It.IsAny<string>())).Verifiable(); 
    }


  [Test]
    public void CheckUserName_Success()
    {
        _vm_Successs.UserName = "Timmy";
        var response = _vm_Successs.CheckUsername();
        sso.Verify(e => e.SendEmailCodeRequest(It.IsAny<PasswordTokenRequest>()), Times.Once);
        nav.Verify(mock => mock.NavigateTo<ForgotPasswordEnterCodeModel, string>(It.IsAny<string>()), Times.Once);
    }

This is the checkusername method

public async Task CheckUsername()
{
            PasswordTokenRequest r = new PasswordTokenRequest();
                await SSOAPIService.SendEmailCodeRequestAsync(r);
            await NavService.NavigateTo<ForgotPasswordEnterCodeModel, string>(UserName);
        }

enter image description here

Upvotes: 1

Views: 1217

Answers (1)

Radik
Radik

Reputation: 2795

you should await test method therefore you need to make your test 'async Task' type

also need to setup SendEmailCodeRequestAsync with ReturnsAsync

    [Test]
    public async Task ShouldDeleteAzureBlobById()
    {
        sso.Setup(x => x.SendEmailCodeRequestAsync(It.IsAny<PasswordTokenRequest>()))
        .ReturnsAsync(new StandardResponse() { ErrorCode = null })
        .Verifiable();

        _vm_Successs.UserName = "Timmy";
        var response = await _vm_Successs.CheckUsername();
        sso.Verify(e => e.SendEmailCodeRequestAsync(It.IsAny<PasswordTokenRequest>()), Times.Once);
        nav.Verify(mock => mock.NavigateTo<ForgotPasswordEnterCodeModel, string>(It.IsAny<string>()), Times.Once);
    }

Upvotes: 1

Related Questions