我想对该方法进行单元测试
SignInViewModel.cs
public ICommand LoginCommand { get; }
private async Task LogIn()
{
var user = _userService.LoginUser(EmailEntry);
if (user == null)
{
await _pageService.DisplayAlert("Alert", "Invalid Credentials", "Ok");
return;
}
if (user.Password != PasswordEntry)
{
await _pageService.DisplayAlert("Alert", "Invalid Credentials", "Ok");
return;
}
PasswordEntry = "";
await _pageService.PushAsync(new HomePage(user));
}
我正在尝试对此进行单元测试,但是没有任何效果,任何人都可以给我示例吗?
答案 0 :(得分:1)
您在这里。
[TestFixture]
public class Fixture
{
private Mock<IUserService> _userService;
private Mock<IPageService> _pageService;
[SetUp]
public void Setup()
{
_userService = new Mock<IUserService>();
_pageService = new Mock<IPageService>();
}
[Test]
public void LoginUserShouldBeCalled()
{
// Given
var vm = CreateInstance();
vm.EmailEntry = "email?";
// When
vm.LoginCommand.Execute(null);
// Then
_userService.Verify(x => x.LoginUser("email?"), Times.Once);
_userService.VerifyNoOtherCalls();
}
[Test]
public void DisplayAlertShouldBeCalledForNotFoundUser()
{
// Given
var vm = CreateInstance();
vm.EmailEntry = "not@found.user";
_userService.Setup(x => x.LoginUser("not@found.user")).Returns((User)null).Verifiable();
// When
vm.LoginCommand.Execute(null);
// Then
_userService.Verify();
_pageService.Verify(x => x.DisplayAlert(It.IsAny<string>(), "Invalid Credentials", It.IsAny<string>()));
_pageService.VerifyNoOtherCalls();
}
[Test]
public void DisplayAlertShouldBeCalledForInvalidPassword()
{
// Given
var vm = CreateInstance();
vm.EmailEntry = "test@email.ru";
vm.PasswordEntry = "password";
_userService.Setup(x => x.LoginUser(It.IsAny<string>())).Returns(new User(vm.EmailEntry, "ppppp")).Verifiable();
// When
vm.LoginCommand.Execute(null);
// Then
_userService.Verify();
_pageService.Verify(x => x.DisplayAlert(It.IsAny<string>(), "Invalid Credentials", It.IsAny<string>()));
_pageService.VerifyNoOtherCalls();
}
[Test]
public void DisplayAlertShouldBeCalledForInvalidPassword2()
{
// Given
var vm = CreateInstance();
vm.EmailEntry = "test@email.ru";
vm.PasswordEntry = "password";
User user = new User(vm.EmailEntry, vm.PasswordEntry);
_userService.Setup(x => x.LoginUser(It.IsAny<string>())).Returns(user).Verifiable();
// When
vm.LoginCommand.Execute(null);
// Then
_userService.Verify();
_pageService.Verify(x => x.PushAsync(It.Is<HomePage>(x => x.User == user)));
_pageService.VerifyNoOtherCalls();
vm.PasswordEntry.Should().BeEmpty();
}
private SignInViewModel CreateInstance()
{
return new SignInViewModel(_userService.Object, _pageService.Object);
}
}
您可以在这里https://github.com/zanybaka/StackOverflow.com/tree/master/Questions/63741243