Пример #1
0
        public ActionResult Save(NutritionViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var userId = this.authenticationProvider.CurrentUserId;

                var nutrition = (Nutrition)null;

                if (model.Id.HasValue)
                {
                    nutrition = this.nutritionService.EditNutrition(userId, model.Date, model.Id.Value, model.Calories, model.Protein, model.Carbs,
                                                                    model.Fats, model.WaterInLitres, model.Fiber, model.Sugar, model.Notes);
                }
                else
                {
                    nutrition = this.nutritionService.CreateNutrition(model.Calories, model.Protein, model.Carbs,
                                                                      model.Fats, model.WaterInLitres, model.Fiber, model.Sugar, model.Notes, userId, model.Date);
                }

                model = this.viewModelFactory.CreateNutritionViewModel(nutrition, model.Date);

                model.SaveResult = Constants.SavedSuccessfully;
            }

            return(this.PartialView("Load", model));
        }
Пример #2
0
        public void TestLoad_ShouldRenderCorrectViewWithModel()
        {
            // Arrange
            var viewModel = new NutritionViewModel();

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(viewModel);

            var mockedDateTimeProvider       = new Mock <IDateTimeProvider>();
            var mockedNutritionService       = new Mock <INutritionService>();
            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            var date = new DateTime(2, 3, 4);

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act, Assert
            controller
            .WithCallTo(c => c.Load(date))
            .ShouldRenderDefaultPartialView()
            .WithModel <NutritionViewModel>(m => Assert.AreSame(viewModel, m));
        }
Пример #3
0
        public void TestSave_ModelStateIsNotValid_ShouldNotCallAuthenticationProviderCurrentUserId(int calories, int protein, int carbs, int fats,
                                                                                                   double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes
            };

            var mockedFactory                = new Mock <IViewModelFactory>();
            var mockedDateTimeProvider       = new Mock <IDateTimeProvider>();
            var mockedNutritionService       = new Mock <INutritionService>();
            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            controller.ModelState.AddModelError("", "");

            // Act
            controller.Save(model);

            // Assert
            mockedAuthenticationProvider.Verify(p => p.CurrentUserId, Times.Never);
        }
Пример #4
0
        public void TestGetNutrition_ServiceReturnsNutrition_ShouldRenderPartialViewWithModel(int id)
        {
            // Arrange
            var date = new DateTime(1, 2, 3);

            var nutrition = new Nutrition {
                Date = date
            };

            var model = new NutritionViewModel();

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(model);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var mockedNutritionService = new Mock <INutritionService>();

            mockedNutritionService.Setup(s => s.GetById(It.IsAny <int>())).Returns(nutrition);

            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act, Assert
            controller
            .WithCallTo(c => c.GetNutrition(id))
            .ShouldRenderPartialView("NutritionDetails")
            .WithModel <NutritionViewModel>(model);
        }
Пример #5
0
        public void TestConstructor_PassNutritionNull_ShouldSetDateCorrectly()
        {
            // Arrange
            var date = new DateTime(1, 2, 3);

            // Act
            var model = new NutritionViewModel(null, date);

            // Assert
            Assert.AreEqual(date, model.Date);
        }
Пример #6
0
        public void TestSave_ModelIdHasValue_ShouldRenderCorrectPartialViewWithModel(int id, int calories, int protein, int carbs, int fats,
                                                                                     double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes,
                Id            = id
            };

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(model);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var nutrition = new Nutrition();

            var mockedNutritionService = new Mock <INutritionService>();

            mockedNutritionService.Setup(s => s.EditNutrition(It.IsAny <string>(), It.IsAny <DateTime>(),
                                                              It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <double>(),
                                                              It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()))
            .Returns(nutrition);

            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId);

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act
            var result = controller.Save(model) as PartialViewResult;

            // Assert
            controller
            .WithCallTo(c => c.Save(model))
            .ShouldRenderPartialView("Load")
            .WithModel <NutritionViewModel>(m => Assert.AreSame(model, m));
        }
Пример #7
0
        public void TestSave_ModelIdHasValue_ShouldCallFactoryCreateNutritionViewModelWithResultFromService(int id, int calories, int protein, int carbs, int fats,
                                                                                                            double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes,
                Id            = id
            };

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(model);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var nutrition = new Nutrition();

            var mockedNutritionService = new Mock <INutritionService>();

            mockedNutritionService.Setup(s => s.EditNutrition(It.IsAny <string>(), It.IsAny <DateTime>(),
                                                              It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <double>(),
                                                              It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()))
            .Returns(nutrition);

            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId);

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act
            controller.Save(model);

            // Assert
            mockedFactory.Verify(f => f.CreateNutritionViewModel(nutrition, date), Times.Once);
        }
Пример #8
0
        public void TestConstructor_PassNutrition_ShouldSetFatsCorrectly(int id, int calories, int protein, int carbs, int fats,
                                                                         double water, int fiber, int sugar, string notes)
        {
            // Arrange
            var date      = new DateTime(1, 2, 3);
            var nutrition = new global::Logs.Models.Nutrition(calories, protein, carbs, fats, water, fiber, sugar, notes, null, date);

            nutrition.NutritionId = id;

            // Act
            var model = new NutritionViewModel(nutrition, date);

            // Assert
            Assert.AreEqual(fats, model.Fats);
        }
Пример #9
0
        public void TestSave_ModelIdDoesNotHaveValue_ShouldSetModelSaveResult(int calories, int protein, int carbs, int fats,
                                                                              double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes
            };

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(model);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var nutrition = new Nutrition();

            var mockedNutritionService = new Mock <INutritionService>();

            mockedNutritionService.Setup(s => s.CreateNutrition(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <double>(),
                                                                It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()))
            .Returns(nutrition);

            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId);

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act
            var result = controller.Save(model) as PartialViewResult;

            // Assert
            Assert.AreEqual(Constants.SavedSuccessfully, ((NutritionViewModel)result.Model).SaveResult);
        }
Пример #10
0
        public void TestSave_ModelIdDoesNotHaveValue_ShouldCallServiceCreateNutrition(int calories, int protein, int carbs, int fats,
                                                                                      double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes
            };

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateNutritionViewModel(It.IsAny <Nutrition>(), It.IsAny <DateTime>()))
            .Returns(model);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();
            var mockedNutritionService = new Mock <INutritionService>();

            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId);

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            // Act
            controller.Save(model);

            // Assert
            mockedNutritionService.Verify(s => s.CreateNutrition(calories, protein, carbs, fats, water, fiber, sugar, notes, userId, date),
                                          Times.Once);
        }
Пример #11
0
        public void TestSave_ModelStateIsNotValid_ShouldRenderCorrectPartialViewWithModel(int calories, int protein, int carbs, int fats,
                                                                                          double water, int fiber, int sugar, string notes, string userId)
        {
            // Arrange,
            var date = new DateTime(2, 3, 4);

            var model = new NutritionViewModel
            {
                Date          = date,
                Calories      = calories,
                Protein       = protein,
                Carbs         = carbs,
                Fats          = fats,
                WaterInLitres = water,
                Fiber         = fiber,
                Sugar         = sugar,
                Notes         = notes
            };

            var mockedFactory                = new Mock <IViewModelFactory>();
            var mockedDateTimeProvider       = new Mock <IDateTimeProvider>();
            var mockedNutritionService       = new Mock <INutritionService>();
            var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>();

            var controller = new NutritionController(mockedFactory.Object, mockedDateTimeProvider.Object,
                                                     mockedNutritionService.Object, mockedAuthenticationProvider.Object);

            controller.ModelState.AddModelError("", "");

            // Act
            controller.Save(model);

            // Assert
            controller
            .WithCallTo(c => c.Save(model))
            .ShouldRenderPartialView("Load")
            .WithModel <NutritionViewModel>(m => Assert.AreSame(model, m));
        }