public async void Create_ReturnsCreated()
        {
            // Arrange
            var controller = new MealController(_mealService.Object, _mapper);
            var expected   = GetSampleMeal();
            var sampleMeal = new InputMealDto
            {
                Name   = expected.Name,
                TypeId = expected.TypeId
            };

            _mealService.Setup(mock => mock.CreateAsync(It.IsAny <Meal>())).ReturnsAsync(expected);

            // Act
            var result = await controller.Create(sampleMeal);

            // Assert
            _mealService.Verify(mock => mock.CreateAsync(It.IsAny <Meal>()), Times.Once);

            var createdResult = Assert.IsType <CreatedAtActionResult>(result);
            var meal          = Assert.IsType <MealDto>(createdResult.Value);

            Assert.Equal(expected.Id, meal.Id);
            Assert.Equal(expected.Name, meal.Name);
            Assert.Equal(expected.TypeId, meal.Type.Id);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] InputMealDto meal)
        {
            // TODO: Fix validation attribute, it's not working as expected.
            if (meal == null)
            {
                return(BadRequest());
            }

            var result = await _mealService.CreateAsync(
                _mapper.Map <Meal>(meal));

            return(CreatedAtAction(
                       nameof(Get),
                       new { id = result.Id },
                       _mapper.Map <MealDto>(result)));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Update(Guid id, [FromBody] InputMealDto meal)
        {
            // TODO: Fix validation attribute, it's not working as expected.
            if (meal == null)
            {
                return(BadRequest());
            }

            var result = await _mealService.UpdateAsync(
                _mapper.Map <Meal>(meal));

            if (result == null)
            {
                return(NotFound());
            }

            return(NoContent());
        }
        public async Task Update_ReturnsNoContent()
        {
            // Arrange
            var controller = new MealController(_mealService.Object, _mapper);
            var expected   = GetSampleMeal();
            var sampleMeal = new InputMealDto
            {
                Name   = expected.Name,
                TypeId = expected.TypeId
            };

            _mealService.Setup(mock => mock.UpdateAsync(It.IsAny <Meal>())).ReturnsAsync(expected);

            // Act
            var result = await controller.Update(expected.Id, sampleMeal);

            // Assert
            _mealService.Verify(mock => mock.UpdateAsync(It.IsAny <Meal>()), Times.Once);
            Assert.IsType <NoContentResult>(result);
        }