正在调用模拟方法,但 Verify returns false

Mocked method being called, but Verify returns false

使用最小起订量,我有一个正在调用的方法,但测试中的验证失败说明它不是。很困惑,因为模拟对象中似乎只有一个调用。通过调试,代码进入了有问题的方法。

测试中的代码

[Fact]
public async Task WhenUsernameAndPasswordAreEmpty_ThenDisplayErrorMessage() {
     mockAuthenticationService.Setup(mock => mock.SignInAsync(It.IsAny<string>(), It.IsAny<string>())).Throws(new NullReferenceException());

     var loginMessageReceived = false;

     mockAppService.Setup(mock => mock.IsBusy).Returns(false);
     mockLoginViewModel.Object.Username = string.Empty;
     mockLoginViewModel.Object.Password = string.Empty;

     MessagingCenter.Subscribe<LoginViewModel>(this, "LoginSuccessful", (obj) => {
          loginMessageReceived = true;
     });

     await mockLoginViewModel.Object.LoginCommand.ExecuteAsync();

     Equals(loginMessageReceived, false);
     mockAuthenticationService.Verify(auth => auth.SignInAsync(string.Empty, string.Empty), Times.Once());
     mockMessageService.Verify(msg => msg.DisplayLoginError(new Exception("You must enter both a username and password to login.")), Times.Once());
}

被调用的代码

 catch (NullReferenceException ex) {
     _messageService.DisplayLoginError(new Exception("You must enter both a username and password to login."));
     var properties = new Dictionary<string, string> {
           { "ExecuteLoginCommand", "You must enter both a username and password to login." },
           { "Username", Username },
           { "Password", Password }
     };
                Crashes.TrackError(ex, properties);
 }

感谢任何指导

好的,部分感谢@canton7 我弄明白了。不得不做一些搜索,但弄清楚了如何。

Verify 需要接受任何类型的 Exception 然后我可以在那里检查 属性。

mockMessageService.Verify(msg => 
    msg.DisplayLoginError(It.Is<Exception>(ex => 
        ex.Message == "You must enter both a username and password to login."
    ))
    , Times.Once()
);