public async Task <IActionResult> OnPostDeleteAsync(int deleteId) { try { await _recipeController.DeleteRecipeByIdAsync(deleteId); } catch (Exception) { return(RedirectToPage("/Error")); } return(RedirectToPage("Index")); }
public async Task DeleteRecipeByIdAsync_Should_Delete_Recipe() { //Arrange List <Recipe> mockRecipeDB = GetRecipes(); int id = 2; _mockRepository.Setup(r => r.GetByIdAsync <Recipe>(id)).ReturnsAsync((int a) => mockRecipeDB.FirstOrDefault(x => x.Id == a)); _mockRepository.Setup(r => r.DeleteAsync <Recipe>(It.Is <Recipe>(entity => entity.Id == id))) .Callback((Recipe recipe) => { mockRecipeDB.Remove(recipe); }); //Act await _recipeController.DeleteRecipeByIdAsync(id); //Assert var mustBeNull = mockRecipeDB.FirstOrDefault(x => x.Id == id); _mockRepository.Verify(r => r.GetByIdAsync <Recipe>(id), Times.Once); _mockRepository.Verify(r => r.DeleteAsync <Recipe>(It.Is <Recipe>(entity => entity.Id == id)), Times.Once); Assert.Null(mustBeNull); }