public async Task CreateRecipe_Returns_Ok_When_Arguments_Are_Valid() { // Arrange var recipesService = Substitute.For <IRecipesService>(); recipesService .CreateAsync(Arg.Any <Guid>(), Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <TimeSpan?>(), Arg.Any <int?>(), Arg.Any <string>(), Arg.Any <IList <Ingredient> >(), Arg.Any <IList <Step> >(), Arg.Any <IList <Tag> >()) .Returns(Task.CompletedTask); var controller = new RecipesController(recipesService, AutoMapper.Mapper.Instance); var recipe = new NewRecipeDTO { Title = "Naleśniki", Description = "Naleśniki z serem", Image = null, Duration = TimeSpan.FromMinutes(30), Servings = 12, Notes = "Idealne na lekki głód", Ingredients = new List <NewRecipeIngredientDTO>(), Steps = new List <NewRecipeStepDTO>(), Tags = new List <NewRecipeTagDTO>() }; // Act var result = await controller.CreateRecipe(recipe); // Assert Assert.True(result.GetType().IsAssignableFrom(typeof(OkResult))); }
public async Task <IActionResult> CreateRecipe([FromBody] NewRecipeDTO recipe) { if (recipe == null || recipe.Ingredients == null || recipe.Steps == null || recipe.Tags == null) { return(BadRequest()); } var userId = Guid.Parse(User.FindFirstValue(KnownClaims.UserId)); var ingredients = recipe.Ingredients.Select(i => Ingredient.Create(i.Name, i.Quantity)).ToList(); var steps = recipe.Steps.Select(s => Step.Create(s.Duration, s.Description)).ToList(); var tags = recipe.Tags.Select(t => Tag.Create(t.Name)).ToList(); await recipesService.CreateAsync( userId, recipe.Title, recipe.IsPrivate, recipe.Description, recipe.Image, recipe.Duration, recipe.Servings, recipe.Notes, ingredients, steps, tags ); return(new StatusCodeResult((int)HttpStatusCode.Created)); }
public async Task CreateRecipe_Returns_BadRequest_When_Arguments_Are_Invalid() { // Arrange var recipesService = Substitute.For <IRecipesService>(); recipesService .CreateAsync(Arg.Any <Guid>(), Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <TimeSpan?>(), Arg.Any <int?>(), Arg.Any <string>(), Arg.Any <IList <Ingredient> >(), Arg.Any <IList <Step> >(), Arg.Any <IList <Tag> >()) .Returns(Task.CompletedTask); var controller = new RecipesController(recipesService, AutoMapper.Mapper.Instance); var recipe = new NewRecipeDTO(); // Act var result = await controller.CreateRecipe(recipe); // Assert Assert.True(result.GetType().IsAssignableFrom(typeof(BadRequestResult))); }