public void Invoke_InvalidData_SaveNorEditAreCalled() { // prepare var appFromDb = new DataAccess.Models.App() { Id = 1 }; var appToEdit = new AppModel() { Id = 1, }; var mockedAppRepository = new Mock <IAppRepository>(); mockedAppRepository.Setup(r => r.GetById(1)) .Returns(appFromDb); var action = new EditApp(mockedAppRepository.Object); // action var actionResult = action.Invoke(appToEdit); // assert Assert.False(actionResult); mockedAppRepository.Verify(r => r.Save(), Times.Never); mockedAppRepository.Verify(r => r.Edit(It.IsAny <DataAccess.Models.App>()), Times.Never); }
public void Invoke_ValidData_DeleteAndSaveAreCalled() { // prepare var appToDelete = new AppModel() { Id = 1 }; var appFromDb = new DataAccess.Models.App() { Id = 1, }; var mockedAppRepository = new Mock <IAppRepository>(); mockedAppRepository.Setup(r => r.GetById(1)).Returns(appFromDb); var action = new DeleteApp(mockedAppRepository.Object); // action var actionResult = action.Invoke(appToDelete.Id); // assert Assert.True(actionResult); mockedAppRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.App>()), Times.Once()); mockedAppRepository.Verify(r => r.Save(), Times.Once()); }
public void Invoke_ValidData_ChangesName() { // prepare var appFromDb = new DataAccess.Models.App() { Id = 1, Name = "Name1", }; var appToEdit = new AppModel() { Id = 1, Name = "Name2", }; DataAccess.Models.App appSaved = null; var mockedAppRepository = new Mock <IAppRepository>(); mockedAppRepository.Setup(r => r.GetById(1)).Returns(appFromDb); mockedAppRepository.Setup(r => r.Edit(It.IsAny <DataAccess.Models.App>())) .Callback <DataAccess.Models.App>(u => appSaved = u); var action = new EditApp(mockedAppRepository.Object); // action var actionResult = action.Invoke(appToEdit); // assert Assert.True(actionResult); Assert.Equal("Name2", appSaved.Name); mockedAppRepository.Verify(r => r.Edit(It.IsAny <DataAccess.Models.App>()), Times.Once); mockedAppRepository.Verify(r => r.Save(), Times.Once); }
public void Invoke_ValidDataForSuperAdmin_ReturnsAllApps() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 2, Name = "app2" }, User = new DataAccess.Models.User { Id = 2, IsSuperAdmin = true } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var app2 = new DataAccess.Models.App { Id = 2, Name = "app2" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1, app2 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <int>())) .Returns(userapp1.User); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(2); // check Assert.Equal(2, list.Count); }
public void Invoke_ValidDataReturnsCorrectModel() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 2, Name = "app2" }, User = new DataAccess.Models.User { Id = 2 } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var app2 = new DataAccess.Models.App { Id = 2, Name = "app2" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1, app2 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(2); // check Assert.Equal(1, list.Count); Assert.Equal(2, list.FirstOrDefault(x => x.Id == 2).Id); Assert.Equal("app2", list.FirstOrDefault(x => x.Name == "app2").Name); }
public int Invoke(AppModel app) { if (!app.IsValid()) { return(0); } var newApp = new DataAccess.Models.App() { Name = app.Name }; appRepository.Add(newApp); appRepository.Save(); return(newApp.Id); }
public void Invoke_InvalidDataReturnsEmptyList() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 1, Name = "app1" }, User = new DataAccess.Models.User { Id = 2 } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(999); // check Assert.Equal(0, list.Count); }
public void Invoke_ValidData_AddAndSaveAreCalled() { // prepare var user = new DataAccess.Models.User() { Id = 1, Email = "test" }; var app = new DataAccess.Models.App() { Id = 1, Name = "test" }; var userAppToAdd = new UserAppModel() { AppId = 1, UserId = 1, Role = Enums.UserRole.SuperAdmin }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); mockedUserRepository.Setup(r => r.GetById(userAppToAdd.UserId)) .Returns(user); mockedAppRepository.Setup(r => r.GetById(userAppToAdd.AppId)) .Returns(app); var action = new AddNewUserApp(mockedUserAppRepository.Object, mockedUserRepository.Object, mockedAppRepository.Object); // action action.Invoke(userAppToAdd); // assert mockedUserAppRepository.Verify(r => r.Add(It.IsAny <DataAccess.Models.UserApps>()), Times.Once()); mockedUserAppRepository.Verify(r => r.Save(), Times.Once()); }
public void Invoke_ReturnsCorrectModel() { // prepare var dataApp = new DataAccess.Models.App() { Id = 1234, Name = "TestApp" }; var findByResult = new List <DataAccess.Models.App> { dataApp }; var mockedAppRepository = new Mock <IAppRepository>(); mockedAppRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.App, bool> > >())) .Returns(findByResult.AsQueryable); var action = new GetApp(mockedAppRepository.Object); // action var app = action.Invoke(1234); // check Assert.Equal(1234, app.Id); Assert.Equal("TestApp", app.Name); }