Exemple #1
0
        public async Task Update_ReturnsNoContent()
        {
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            var id       = Guid.NewGuid();
            var lunch    = GetSampleLunch(id);
            var lunchDto = new InputLunchDto
            {
                Date   = lunch.Date,
                MealId = lunch.MealId
            };

            _lunchService.Setup(a => a.UpdateAsync(
                                    It.Is <Lunch>(l =>
                                                  l.MealId == lunchDto.MealId &&
                                                  l.Date == lunchDto.Date &&
                                                  l.Id == id)))
            .ReturnsAsync(lunch);

            var result = await classUnderTest.Update(id, lunchDto);

            Assert.IsType <NoContentResult>(result);

            _lunchService.Verify(a => a.UpdateAsync(
                                     It.Is <Lunch>(l =>
                                                   l.MealId == lunchDto.MealId &&
                                                   l.Date == lunchDto.Date &&
                                                   l.Id == id)), Times.Once);
        }
Exemple #2
0
        public async Task Create_ReturnsCreated()
        {
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);
            var lunch          = GetSampleLunch();

            var lunchDto = new InputLunchDto
            {
                Date   = lunch.Date,
                MealId = lunch.MealId
            };

            _lunchService.Setup(a => a.CreateAsync(
                                    It.Is <Lunch>(l => l.MealId == lunchDto.MealId && l.Date == lunchDto.Date)))
            .ReturnsAsync(lunch);

            var result = await classUnderTest.Create(lunchDto);

            var createdResponse = Assert.IsType <CreatedAtActionResult>(result);
            var resultLunch     = Assert.IsType <LunchDto>(createdResponse.Value);

            Assert.Equal(lunch.MealId, resultLunch.Meal.Id);
            Assert.Equal(lunch.Date, resultLunch.Date);

            _lunchService.Verify(a => a.CreateAsync(
                                     It.Is <Lunch>(l => l.MealId == lunchDto.MealId && l.Date == lunchDto.Date)), Times.Once);
        }
        public async Task <IActionResult> Create([FromBody] InputLunchDto lunch)
        {
            // TODO: Fix validation attribute, it's not working as expected.
            if (lunch == null)
            {
                return(BadRequest());
            }

            var result = await _lunchService.CreateAsync(
                _mapper.Map <Lunch>(lunch));

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

            var lunch = _mapper.Map <Lunch>(lunchDto);

            lunch.Id = id;

            var result = await _lunchService.UpdateAsync(lunch);

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

            return(NoContent());
        }