public async Task Controller_GetTaskById_Test() { //arrange var task = new TaskAddEditViewModel() { Name = "Concrete task", Comment = "Task for test", PriorityId = Priority.High, DueDate = DateTime.Now.AddDays(1) }; var repositoryMock = new Mock <ITaskRepository>(); repositoryMock.Setup(x => x.GetTask(It.IsAny <int>())) .Returns(Task.FromResult(task)); var userServiceMock = new Mock <IUserService>(); var controller = new TasksApiController(repositoryMock.Object, userServiceMock.Object); //act var resultCorrectId = await controller.GetTaskById(12); var resultIncorrectId = await controller.GetTaskById(0); //assert Assert.IsNotNull(resultCorrectId); Assert.IsNotNull(resultIncorrectId); Assert.IsInstanceOfType(resultCorrectId, typeof(OkObjectResult)); Assert.IsInstanceOfType(resultIncorrectId, typeof(BadRequestResult)); Assert.AreEqual(task.Name, ((resultCorrectId as OkObjectResult).Value as TaskAddEditViewModel).Name); repositoryMock.Verify(x => x.GetTask(It.IsAny <int>()), Times.Once); }
public async Task Repository_AddTask_Test() { //arrange MapperHelper.InitializeMapper(); var vm = new TaskAddEditViewModel() { Name = "test task", Comment = "test comment", PriorityId = Priority.High }; var user = new ProjectUser { Id = "qwer" }; var contextMock = new Mock <IProjectContext>(); var dbsetMock = CreateDbSetMock(new List <TaskModel>()); contextMock.Setup(x => x.Tasks).Returns(dbsetMock.Object); var userServiceMock = new Mock <IUserService>(); userServiceMock.Setup(x => x.GetUserById(It.IsAny <string>())) .Returns(Task.FromResult(user)); var repository = new TaskRepository(contextMock.Object, userServiceMock.Object); //act var result = await repository.AddEditTask(vm); //assert Assert.IsTrue(result); Assert.AreEqual(1, contextMock.Object.Tasks.Count(), "Object wasn't added to DbSet"); Assert.IsNotNull(contextMock.Object.Tasks.SingleOrDefault(), "Object was added to DbSet more than one time"); }
public async Task Controller_AddEditTest_Test() { //arrange TaskAddEditViewModel correctTask = new TaskAddEditViewModel { TaskId = 14, Name = "testName", DueDate = DateTime.Now, Comment = "comment", PriorityId = Priority.Low }; TaskAddEditViewModel errorTask = new TaskAddEditViewModel { TaskId = -1, Name = "error", DueDate = DateTime.Now, Comment = "comment", PriorityId = Priority.Low }; TaskAddEditViewModel incorrectTask = null; var repositoryMock = new Mock <ITaskRepository>(); repositoryMock.Setup(x => x.AddEditTask(correctTask)) .Returns(Task.FromResult(true)); repositoryMock.Setup(x => x.AddEditTask(errorTask)) .Returns(Task.FromResult(false)); repositoryMock.Setup(x => x.SaveChanges()) .Returns(Task.FromResult(true)); var userServiceMock = new Mock <IUserService>(); userServiceMock.Setup(x => x.GetCurrentUserId(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult(It.IsAny <string>())); var controller = new TasksApiController(repositoryMock.Object, userServiceMock.Object); //act var resultCorrectModel = await controller.AddEditTask(correctTask); var resultModelError = await controller.AddEditTask(incorrectTask); var resultDataSavingError = await controller.AddEditTask(errorTask); //assert Assert.IsNotNull(resultModelError); Assert.IsNotNull(resultCorrectModel); Assert.IsNotNull(resultDataSavingError); Assert.IsInstanceOfType(resultCorrectModel, typeof(OkResult)); Assert.IsInstanceOfType(resultModelError, typeof(BadRequestResult)); Assert.IsInstanceOfType(resultDataSavingError, typeof(BadRequestResult)); repositoryMock.Verify(x => x.AddEditTask(It.IsAny <TaskAddEditViewModel>()), Times.Exactly(2)); repositoryMock.Verify(x => x.SaveChanges(), Times.Once); }
public async Task Repository_EditTask_Test() { //arrange MapperHelper.InitializeMapper(); var vm = new TaskAddEditViewModel { TaskId = 3, Name = "Edited name", Comment = "edited comment", DueDate = DateTime.UtcNow.AddDays(2), PriorityId = Priority.Medium }; var task = new TaskModel { TaskId = 3, Name = "not edited name", Comment = "not edited comment", PriorityId = Priority.High, DueDate = DateTime.UtcNow }; var user = new ProjectUser { Id = "qwer" }; var sourceList = new List <TaskModel>() { task }; var contextMock = new Mock <IProjectContext>(); contextMock.Setup(x => x.Tasks).Returns(CreateDbSetMock(sourceList).Object); var userServiceMock = new Mock <IUserService>(); userServiceMock.Setup(x => x.GetUserById(It.IsAny <string>())) .Returns(Task.FromResult(user)); var repository = new TaskRepository(contextMock.Object, userServiceMock.Object); //act var result = await repository.AddEditTask(vm); //assert Assert.IsTrue(result); var editedObj = contextMock.Object.Tasks.FirstOrDefault(x => x.TaskId == vm.TaskId); Assert.AreEqual(vm.Name, editedObj.Name); Assert.AreEqual(vm.Comment, editedObj.Comment); Assert.AreEqual(vm.DueDate, editedObj.DueDate); Assert.AreEqual(vm.PriorityId, editedObj.PriorityId); }
public async Task <bool> AddEditTask(TaskAddEditViewModel model) { bool result = false; if (model.TaskId == 0) { result = AddTask(model); } else { result = EditTask(model); } return(result); }
private bool EditTask(TaskAddEditViewModel task) { try { var entity = _projectContext.Tasks.FirstOrDefault(x => x.TaskId == task.TaskId); entity = Mapper.Map(task, entity); return(true); } catch (Exception ex) { //TODO: Implement logging return(false); } }
private bool AddTask(TaskAddEditViewModel task) { try { //await _projectContext.Tasks.AddAsync(task); - not working var entity = Mapper.Map <TaskModel>(task); _projectContext.Tasks.Add(entity); return(true); } catch (Exception ex) { //TODO: Implement logging return(false); } }
public async Task <IActionResult> AddEditTask([FromBody] TaskAddEditViewModel model) { if (!ModelState.IsValid || model == null) { return(BadRequest()); } var userId = await _userService.GetCurrentUserId(User); model.UserId = userId; var completed = await _taskRepository.AddEditTask(model); if (completed) { completed = completed && await _taskRepository.SaveChanges(); } if (completed) { return(Ok()); } ModelState.AddModelError(string.Empty, "Changes wasn't saved"); return(BadRequest()); }