public void ShouldCreateMealCorrectly()
        {
            // Arrange
            Mock <IMealRepository>    mealMock    = new Mock <IMealRepository>();
            Mock <IStudentRepository> studentMock = new Mock <IStudentRepository>();

            Student student = new Student()
            {
                Id        = "101",
                FirstName = "Ginni",
                LastName  = "Wassell"
            };

            Meal meal = new Meal()
            {
                Id          = 100,
                Title       = "Compound - Strawberry",
                Description = " ",
                Price       = 1.50,
                Date        = DateTime.Now.AddDays(6),
                MaxGuests   = 1,
            };

            ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, student.Id) }));

            meal.Cook = student;
            student.CookAtMeals.Add(meal);

            mealMock.Setup(m => m.Meals).Returns(new List <Meal>()
            {
                meal
            });

            mealMock.Setup(m => m.Create(It.IsAny <Meal>())).Returns((Meal model) =>
            {
                if (meal.Title != null && meal.MaxGuests > 0 && meal.Price >= 0)
                {
                    return(true);
                }

                return(false);
            });

            MealController controller = new MealController(mealMock.Object, studentMock.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = identity
                    }
                }
            };

            // Act
            RedirectResult redirectResult = controller.Create(meal) as RedirectResult;

            // Assert
            Assert.Equal("/Meal", redirectResult?.Url);
        }
        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);
        }
        public async Task Create_ReturnsBadRequest_WhenMealIsNull()
        {
            // Arrange
            var classUnderTest = new MealController(_mealService.Object, _mapper);

            // Act
            var res = await classUnderTest.Create(null);

            // Assert
            Assert.IsType <BadRequestResult>(res);
        }
        public void CannotEditMealWithNoTitle()
        {
            Mock <IMealRepository>    mealMock    = new Mock <IMealRepository>();
            Mock <IStudentRepository> studentMock = new Mock <IStudentRepository>();

            Meal meal = new Meal()
            {
                Id          = 100,
                Title       = null,
                Description = " ",
                Price       = 1.50,
                Date        = DateTime.Now.AddDays(-1),
                MaxGuests   = 1
            };

            mealMock.Setup(m => m.Meals).Returns(new List <Meal>()
            {
                meal
            });

            mealMock.Setup(m => m.Create(It.IsAny <Meal>())).Returns((Meal model) =>
            {
                if (meal.Date > DateTime.Now)
                {
                    return(true);
                }

                return(false);
            });

            mealMock.Setup(m => m.Update(It.IsAny <Meal>())).Returns((Meal model) =>
            {
                if (meal.Title != null && meal.MaxGuests > 0 && meal.Price >= 0)
                {
                    return(true);
                }

                return(false);
            });

            MealController controller = new MealController(mealMock.Object, studentMock.Object);

            // Act
            controller.Create(meal);
            meal.Title = null;
            ViewResult viewResult = controller.Edit(meal) as ViewResult;

            // Assert
            Assert.Null(viewResult.ViewName);
        }
        public void CannotCreateMealWithZeroMaxGuests()
        {
            Mock <IMealRepository>    mealMock    = new Mock <IMealRepository>();
            Mock <IStudentRepository> studentMock = new Mock <IStudentRepository>();

            Meal meal = new Meal()
            {
                Id          = 100,
                Title       = "Compound - Strawberry",
                Description = " ",
                Price       = 1.50,
                Date        = DateTime.Now.AddDays(-1),
                MaxGuests   = 0
            };

            mealMock.Setup(m => m.Meals).Returns(new List <Meal>()
            {
                meal
            });

            mealMock.Setup(m => m.Create(It.IsAny <Meal>())).Returns((Meal model) =>
            {
                if (meal.Date > DateTime.Now)
                {
                    return(true);
                }

                return(false);
            });

            MealController controller = new MealController(mealMock.Object, studentMock.Object);

            // Act
            ViewResult viewResult = controller.Create(meal) as ViewResult;

            // Assert
            Assert.Null(viewResult.ViewName);
        }