Moq验证失败 - 批处理运行时单元测试失败,单独运行时成功

时间:2016-03-11 13:58:38

标签: unit-testing asynchronous moq

我发现这个单元测试行为非常奇怪。如果我自己运行这个单元测试,那么每次都是成功的。

如果我将它与同一类中的其他测试一起运行,则会失败。我可以理解它是否在运行时与其他测试共享,但据我所知,此测试中使用的所有对象都是专门为此测试创建的。

public void AttemptLoginFailsEmailValidates()
{
    const string emailAddress = "test@test.com";
    const string password = "Password";

    ClearAll();

    var moqSettingsService = new Mock<ISettingsService>();
    var moqLoginService = new Mock<ILoginService>();
    var moqMessenger = new Mock<IMvxMessenger>();
    var moqDialogService = new Mock<IDialogService>();

    var validationService = new ValidatorService();
    moqLoginService.Setup(li => li.Login(It.Is<string>(s => s == emailAddress), It.Is<string>(s => s == password), false, It.Is<string>(s => s == "User"))).Returns(Task.FromResult(false));

    var vm = new LoginViewModel(moqLoginService.Object, moqSettingsService.Object, validationService, moqDialogService.Object, moqMessenger.Object) { EmailLogin = emailAddress, Password = password };

    Assert.AreEqual(emailAddress, vm.EmailLogin);
    Assert.AreEqual(password, vm.Password);

    vm.LoginCommand.Execute(new object());

    moqLoginService.Verify(p => p.Login(It.Is<string>(s => s == emailAddress), It.Is<string>(s => s == password), false, It.Is<string>(s => s == "User")), Times.Once);
}

Moq例外是:

Moq.MockException : 
Expected invocation on the mock once, but was 0 times: p => p.Login(It.Is<String>(s => s == "test@test.com"), It.Is<String>(s => s == "Password"), False, It.Is<String>(s => s == "User"))

Configured setups:
li => li.Login(It.Is<String>(s => s == "test@test.com"), It.Is<String>(s => s == "Password"), False, It.Is<String>(s => s == "User")), Times.Never
No invocations performed.

0 个答案:

没有答案