C# 正在调用模拟方法,但验证返回false
使用Moq,我有一个被调用的方法,但是测试中的Verify失败,表明它不是。很困惑,因为在模拟对象中似乎有一个调用。经过一步一步的调试,代码将其转换为所讨论的方法 测试中的代码C# 正在调用模拟方法,但验证返回false,c#,xamarin.forms,moq,xunit.net,C#,Xamarin.forms,Moq,Xunit.net,使用Moq,我有一个被调用的方法,但是测试中的Verify失败,表明它不是。很困惑,因为在模拟对象中似乎有一个调用。经过一步一步的调试,代码将其转换为所讨论的方法 测试中的代码 [Fact] public async Task WhenUsernameAndPasswordAreEmpty_ThenDisplayErrorMessage() { mockAuthenticationService.Setup(mock => mock.SignInAsync(It.IsAny<
[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());
}
[事实]
当用户名和密码为空时的公共异步任务\u然后显示错误消息(){
mockAuthenticationService.Setup(mock=>mock.SignInAsync(It.IsAny(),It.IsAny()).Throws(new NullReferenceException());
var loginMessageReceived=false;
mockAppService.Setup(mock=>mock.IsBusy).返回(false);
mockLoginViewModel.Object.Username=string.Empty;
mockLoginViewModel.Object.Password=string.Empty;
MessagingCenter.Subscribe(此“登录成功”,(obj)=>{
loginMessageReceived=true;
});
等待mockLoginViewModel.Object.LoginCommand.ExecuteAsync();
等于(loginMessageReceived,false);
mockAuthenticationService.Verify(auth=>auth.SignInAsync(string.Empty,string.Empty),Times.Once());
mockMessageService.Verify(msg=>msg.DisplayLoginError(新的异常(“必须输入用户名和密码才能登录”))、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);
}
catch(NullReferenceException-ex){
_messageService.DisplayLoginError(新的异常(“您必须输入用户名和密码才能登录”);
var属性=新字典{
{“ExecuteLoginCommand”,“您必须输入用户名和密码才能登录。”},
{“用户名”,用户名},
{“密码”,密码}
};
崩溃。跟踪错误(例如,属性);
}
感谢您的指导好的,部分感谢@canton7我找到了答案。不得不做一些搜索,但找到了方法
Verify
需要接受任何类型的异常
,然后我可以检查那里的属性
mockMessageService.Verify(msg =>
msg.DisplayLoginError(It.Is<Exception>(ex =>
ex.Message == "You must enter both a username and password to login."
))
, Times.Once()
);
mockMessageService.Verify(msg=>
msg.DisplayLoginError(It.Is(ex=>
例如,Message==“您必须输入用户名和密码才能登录。”
))
,Times.Once()
);
当然,您传递给Verify
的异常
实例与传递给DisplayLoginError
@canton7的实例不同,所以我明白您的意思,但对于Moq来说是新手,我如何测试以查看调用该方法的输入参数?设置期望,它捕获了所讨论的异常。然后对它运行一些断言,例如检查它的消息
,这比我的建议好,很好@SHAGGY13SPEE这应该对提高Moq的速度有所帮助。请您将您的回答标记为一个答案,以便其他有类似问题的人可以从您的答案中获得帮助。抱歉@JessieZhang MSFT,必须等待至少一天才能让我和我忘记它