public void PostRecipeTest() { Recipe recipe = new Recipe(); _recipesService.Setup(i => i.AddRecipe(recipe)); var result = _recipesController.PostRecipe(recipe).Result; _recipesService.Verify(i => i.AddRecipe(recipe), Times.Once); Assert.IsInstanceOf <CreatedAtActionResult>(result); }
public async Task PostRecipe_Sets_LocationHeader() { // Arrange var mockRepository = new Mock <IRecipesRepository>(); var controller = new RecipesController(mockRepository.Object); // Act Task <IHttpActionResult> actionResult = controller.PostRecipe(_testData.RecipeData); var createdResult = await actionResult as CreatedAtRouteNegotiatedContentResult <Recipe>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual("DefaultApi", createdResult.RouteName); Assert.AreEqual(_testData.RecipeData.Id, createdResult.RouteValues["id"]); }
public async Task TestPostRecipe() { using (var context = new MyRecipesContext(options)) { Recipe recipe = new Recipe(); RecipesController recipeController = new RecipesController(context, configuration); IActionResult okResult = await recipeController.PostRecipe(recipe) as IActionResult; // Assert Assert.IsNotNull(okResult); Assert.IsInstanceOfType(okResult, typeof(CreatedAtActionResult)); } }
public async Task PostRecipeWithoutIngredients() { var options = _fixture.options; var recipesContext = new RecipesContext(options); var controller = new RecipesController(recipesContext); var ingredientList = Array.Empty <IngredientDto>(); var newRecipe = new RecipeDto("Test recipe", "Test description", ingredientList); await controller.PostRecipe(newRecipe); var addedRecipe = recipesContext.Recipes.Last(); Assert.Equal("Test recipe", addedRecipe.Name); Assert.Equal("Test description", addedRecipe.Description); Assert.Empty(addedRecipe.Ingredients); }
public async Task PostRecipeWithIngredients() { var options = _fixture.options; var recipesContext = new RecipesContext(options); var controller = new RecipesController(recipesContext); var newIngredient = new IngredientDto("ui", 2, "unit"); var newIngredient2 = new IngredientDto("knoflook", 3, "unit"); var ingredientList = new[] { newIngredient, newIngredient2 }; var newRecipe = new RecipeDto("Test recipe", "Test description", ingredientList); await controller.PostRecipe(newRecipe); var addedRecipe = recipesContext.Recipes.Last(); Assert.Equal("Test recipe", addedRecipe.Name); Assert.Equal("Test description", addedRecipe.Description); Assert.Equal(2, addedRecipe.Ingredients.Count); }
public async Task TestPostRecipeAlreadyExist() { using (var context = new MyRecipesContext(options)) { string name = "postRecipe"; Recipe recipe1 = context.Recipe.Where(x => x.Name == recipeNames[0]).Single(); recipe1.Name = name; try { RecipesController recipeController = new RecipesController(context, configuration); IActionResult okResult = await recipeController.PostRecipe(recipe1) as IActionResult; } catch (Exception e) { // Assert Assert.IsInstanceOfType(e, typeof(ArgumentException)); } } }
public async Task PostRecipeWithIngredient() { var options = _fixture.options; var recipesContext = new RecipesContext(options); var controller = new RecipesController(recipesContext); var newIngredient = new IngredientDto("ui", 2, "unit"); var ingredientList = new[] { newIngredient }; var newRecipe = new RecipeDto("Test recipe", "Test description", ingredientList); await controller.PostRecipe(newRecipe); var addedRecipe = recipesContext.Recipes.LastOrDefault(); Assert.Equal("Test recipe", addedRecipe.Name); Assert.Equal("Test description", addedRecipe.Description); var firstIngredient = addedRecipe.Ingredients[0]; Assert.Equal("ui", firstIngredient.Name); Assert.Equal(2, firstIngredient.Amount); Assert.Equal("unit", firstIngredient.Unit); }
public async Task PostRecipe_Returns_CreatedResult() { // Arrange var mockRepository = new Mock <IRecipesRepository>(); var controller = new RecipesController(mockRepository.Object); // Act Task <IHttpActionResult> actionResult = controller.PostRecipe(_testData.RecipeData); var createdResult = await actionResult as CreatedAtRouteNegotiatedContentResult <Recipe>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual(_testData.RecipeData.Id, createdResult.Content.Id); Assert.AreEqual(_testData.RecipeData.Name, createdResult.Content.Name); Assert.AreEqual(_testData.RecipeData.Description, createdResult.Content.Description); Assert.AreEqual(_testData.RecipeData.Ingredients, createdResult.Content.Ingredients); Assert.AreEqual(_testData.RecipeData.Directions, createdResult.Content.Directions); Assert.AreEqual(_testData.RecipeData.Type, createdResult.Content.Type); Assert.AreEqual(_testData.RecipeData.ImageLink, createdResult.Content.ImageLink); Assert.AreEqual(_testData.RecipeData.DateCreated, createdResult.Content.DateCreated); Assert.AreEqual(_testData.RecipeData.Notes, createdResult.Content.Notes); }
public async void Can_post_recipe() { using var context = new RecAPIContext(ContextOptions); var controller = new RecipesController(context); var riFusilliDTO = new RecipeIngredientDTO { IngredientId = 2, IngredientName = "Fusilli", Amount = 200, Unit = (Unit)1 }; var riKetchupDTO = new RecipeIngredientDTO { IngredientId = 3, IngredientName = "Ketchup", Amount = 200, Unit = (Unit)1 }; var recipeDTO = new RecipeDTO { Name = "Pasta with ketchup", Description = "Better than with garlic", Instructions = "Boil pasta. Add ketchup.", RecipeIngredients = new List <RecipeIngredientDTO> { riFusilliDTO, riKetchupDTO } }; var recipeActionResult = await controller.PostRecipe(recipeDTO); var recipe = (recipeActionResult.Result as CreatedAtActionResult).Value as RecipeDTO; Assert.Equal("Pasta with ketchup", recipe.Name); Assert.Equal("Fusilli", recipe.RecipeIngredients.FirstOrDefault().IngredientName); }