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)); }
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)); }
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); }
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); }
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); }
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)); }
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); }
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); }
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); }
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); }
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)); }