У меня есть этот тестовый класс, и когда я запускаю эти тесты по одному, они проходят, но когда я пытаюсь запустить все тесты из этого класса, первый тест всегда проходит, а другие обычно не проходят (иногда случайно один из них проходит) икогда я запускаю все тесты из моего проекта, все эти тесты не выполняются.
Я использую платформы NUnit и Moq.
Вот код:
using System.Security;
using DebtDiary.Core;
using DebtDiary.DataProvider;
using Moq;
using NUnit.Framework;
namespace DebtDiary.Tests.ViewModels
{
[TestFixture]
public class LoginPageViewModelTests
{
[Test]
public void TestLoginCommandCallsLoginUserInClientDataStoreWhenDataIsValid()
{
Mock<IApplicationViewModel> stubApplicationVM = new Mock<IApplicationViewModel>();
Mock<IDiaryPageViewModel> stubDiaryPageVM = new Mock<IDiaryPageViewModel>();
Mock<IDialogFacade> stubDialogFacadeVM = new Mock<IDialogFacade>();
Mock<IClientDataStore> mockClientDataStore = new Mock<IClientDataStore>();
Mock<IDataAccess> stubDataAccess = new Mock<IDataAccess>();
var loginPageVM = new LoginPageViewModel(stubApplicationVM.Object, stubDiaryPageVM.Object, stubDialogFacadeVM.Object, mockClientDataStore.Object, stubDataAccess.Object);
loginPageVM.Username = "test";
Mock<IHavePassword> stubPassword = new Mock<IHavePassword>();
SecureString ss = new SecureString();
ss.AppendChar('t');
stubPassword.Setup(x => x.Password).Returns(ss);
User user = new User();
stubDataAccess.Setup(x => x.UserExist(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
stubDataAccess.Setup(x => x.TryGetUser(It.IsAny<string>(), It.IsAny<string>(), out user)).Returns(true);
loginPageVM.LoginCommand.Execute(stubPassword.Object);
mockClientDataStore.Verify(x => x.LoginUser(It.IsAny<User>()), Times.Once());
}
[Test]
public void TestLoginCommandUpdatesDebtorsListInDiaryPageViewModelWhenDataIsValid()
{
Mock<IApplicationViewModel> stubApplicationVM = new Mock<IApplicationViewModel>();
Mock<IDiaryPageViewModel> mockDiaryPageVM = new Mock<IDiaryPageViewModel>();
Mock<IDialogFacade> stubDialogFacadeVM = new Mock<IDialogFacade>();
Mock<IClientDataStore> stubClientDataStore = new Mock<IClientDataStore>();
Mock<IDataAccess> stubDataAccess = new Mock<IDataAccess>();
var loginPageVM = new LoginPageViewModel(stubApplicationVM.Object, mockDiaryPageVM.Object, stubDialogFacadeVM.Object, stubClientDataStore.Object, stubDataAccess.Object);
loginPageVM.Username = "test";
Mock<IHavePassword> stubPassword = new Mock<IHavePassword>();
SecureString ss = new SecureString();
ss.AppendChar('t');
stubPassword.Setup(x => x.Password).Returns(ss);
User user = new User();
stubDataAccess.Setup(x => x.UserExist(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
stubDataAccess.Setup(x => x.TryGetUser(It.IsAny<string>(), It.IsAny<string>(), out user)).Returns(true);
loginPageVM.LoginCommand.Execute(stubPassword.Object);
mockDiaryPageVM.Verify(x => x.UpdateDebtorsList(), Times.Once());
}
[Test]
public void TestLoginCommandUpdatesUsersDataInDiaryPageViewModelWhenDataIsValid()
{
Mock<IApplicationViewModel> stubApplicationVM = new Mock<IApplicationViewModel>();
Mock<IDiaryPageViewModel> mockDiaryPageVM = new Mock<IDiaryPageViewModel>();
Mock<IDialogFacade> stubDialogFacadeVM = new Mock<IDialogFacade>();
Mock<IClientDataStore> stubClientDataStore = new Mock<IClientDataStore>();
Mock<IDataAccess> stubDataAccess = new Mock<IDataAccess>();
var loginPageVM = new LoginPageViewModel(stubApplicationVM.Object, mockDiaryPageVM.Object, stubDialogFacadeVM.Object, stubClientDataStore.Object, stubDataAccess.Object);
loginPageVM.Username = "test";
Mock<IHavePassword> stubPassword = new Mock<IHavePassword>();
SecureString ss = new SecureString();
ss.AppendChar('t');
stubPassword.Setup(x => x.Password).Returns(ss);
User user = new User();
stubDataAccess.Setup(x => x.UserExist(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
stubDataAccess.Setup(x => x.TryGetUser(It.IsAny<string>(), It.IsAny<string>(), out user)).Returns(true);
loginPageVM.LoginCommand.Execute(stubPassword.Object);
mockDiaryPageVM.Verify(x => x.UpdateUsersData(), Times.Once());
}
[Test]
public void TestLoginCommandResetsCurrentSubpageInApplicationViewModelWhenDataIsValid()
{
Mock<IApplicationViewModel> mockApplicationVM = new Mock<IApplicationViewModel>();
Mock<IDiaryPageViewModel> stubDiaryPageVM = new Mock<IDiaryPageViewModel>();
Mock<IDialogFacade> stubDialogFacadeVM = new Mock<IDialogFacade>();
Mock<IClientDataStore> stubClientDataStore = new Mock<IClientDataStore>();
Mock<IDataAccess> stubDataAccess = new Mock<IDataAccess>();
var loginPageVM = new LoginPageViewModel(mockApplicationVM.Object, stubDiaryPageVM.Object, stubDialogFacadeVM.Object, stubClientDataStore.Object, stubDataAccess.Object);
loginPageVM.Username = "test";
Mock<IHavePassword> stubPassword = new Mock<IHavePassword>();
SecureString ss = new SecureString();
ss.AppendChar('t');
stubPassword.Setup(x => x.Password).Returns(ss);
User user = new User();
stubDataAccess.Setup(x => x.UserExist(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
stubDataAccess.Setup(x => x.TryGetUser(It.IsAny<string>(), It.IsAny<string>(), out user)).Returns(true);
loginPageVM.LoginCommand.Execute(stubPassword.Object);
mockApplicationVM.Verify(x => x.ResetCurrentSubpage(), Times.Once());
}
}
}
Знаете ли выв чем может быть причина?Как видите, я переместил весь повторяемый код в эти методы, чтобы избежать зависимостей, и он не работает.