Пример #1
0
        public async Task GetAll_ReturnsLunches()
        {
            // Arrange
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            var listOfLunches = new List <Lunch>()
            {
                GetSampleLunch(),
                GetSampleLunch(),
                GetSampleLunch()
            };

            _lunchService.Setup(a => a.GetAsync()).ReturnsAsync(listOfLunches);
            // Act
            var result = await classUnderTest.GetAll();

            // Assert
            var okResult   = Assert.IsType <OkObjectResult>(result);
            var resultList = Assert.IsAssignableFrom <IEnumerable <LunchDto> >(okResult.Value);

            foreach (var lunch in listOfLunches)
            {
                Assert.Contains(resultList, resultLunch => Equals(resultLunch, lunch));
            }

            _lunchService.Verify(a => a.GetAsync(), Times.Once);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
        public async Task Update_ReturnsBadRequest_WhenDtoIsNull()
        {
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            var result = await classUnderTest.Update(Guid.NewGuid(), null);

            Assert.IsType <BadRequestResult>(result);
            _lunchService.Verify(a => a.UpdateAsync(It.IsAny <Lunch>()), Times.Never);
        }
Пример #5
0
        public async Task Delete_ReturnsNotFound_WhenIdNotExist()
        {
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            var id = Guid.NewGuid();

            _lunchService.Setup(a => a.DeleteByIdAsync(It.Is <Guid>(g => g == id)))
            .ReturnsAsync(0);

            var result = await classUnderTest.Delete(id);

            Assert.IsType <NotFoundResult>(result);
            _lunchService.Verify(s => s.DeleteByIdAsync(It.IsAny <Guid>()), Times.Once);
        }
Пример #6
0
        public async Task Get_ReturnsNotFound_WhenIdNotExist()
        {
            // Arrange
            var id             = Guid.NewGuid();
            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            _lunchService.Setup(a => a.GetByIdAsync(It.Is <Guid>(g => g == id)))
            .ReturnsAsync(null as Lunch);

            // Assert
            var result = await classUnderTest.Get(id);

            // Act
            Assert.IsType <NotFoundResult>(result);

            _lunchService.Verify(a => a.GetByIdAsync(It.Is <Guid>(g => g == id)), Times.Once);
        }
Пример #7
0
        public async Task Get_ReturnsALunch()
        {
            // Arrange
            var id    = Guid.NewGuid();
            var lunch = GetSampleLunch(id);

            var classUnderTest = new LunchController(_lunchService.Object, _mapper);

            _lunchService.Setup(a => a.GetByIdAsync(It.Is <Guid>(g => g == id)))
            .ReturnsAsync(lunch);

            // Act
            var result = await classUnderTest.Get(id);

            // Assert
            var okResult    = Assert.IsType <OkObjectResult>(result);
            var lunchResult = Assert.IsType <LunchDto>(okResult.Value);

            Assert.True(Equals(lunchResult, lunch));

            _lunchService.Verify(a => a.GetByIdAsync(It.Is <Guid>(g => g == id)), Times.Once);
        }