public void EditPost_ConcurrencyInUpdateAndToDoListExists_ThrowsDbUpdateConcurrencyException() { //Arrange const int toDoListId = 1; ToDoList toDoList = CreateToDoListDefault(); IToDoListService toDoListServiceFake = A.Fake <IToDoListService>(); DbUpdateConcurrencyException dbUpdateConcurrencyException = new DbUpdateConcurrencyException( "Update concurrency exception", new List <IUpdateEntry> { A.Fake <IUpdateEntry>() }); A.CallTo(() => toDoListServiceFake.Update(A <ToDoList> .Ignored)) .ThrowsAsync(dbUpdateConcurrencyException); A.CallTo(() => toDoListServiceFake.Exists(A <int> .Ignored)) .Returns(true); ToDoListController sut = CreateSut(toDoListServiceFake); //Act Func <Task <IActionResult> > action = () => sut.Edit(toDoListId, toDoList); //Assert action .Should() .Throw <DbUpdateConcurrencyException>(); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] ToDoList toDoList) { if (id != toDoList.Id) { return(NotFound()); } if (!ModelState.IsValid) { return(View(toDoList)); } try { await _toDoListService.Update(toDoList); } catch (DbUpdateConcurrencyException) { var todoExists = await _toDoListService.Exists(id); if (!todoExists) { return(NotFound()); } throw; } this.AddAlertSuccess($"{toDoList.Name} updated."); return(RedirectToAction(nameof(Index))); }