public void shouldAddIngredientToCurrentUser() { var mockFactory = new MockFactory(MockBehavior.Loose); var userContextMock = GetSetuppedUserContextMock(mockFactory); var userIngredientBusinessLogicMock = mockFactory.Create<IUserIngredientBusinessLogic>(); var userIngredient = new UserIngredient { User = new User {Username = "******"}, Ingredient = new Ingredient {Name = "Pannbiff"}, Measure = 100 }; userIngredientBusinessLogicMock.Setup(x => x.GetUserIngredients(It.IsAny<User>(), It.IsAny<DateTime>())).Returns(new[] {userIngredient}); userIngredientBusinessLogicMock.Setup(x => x.AddUserIngredient(It.Is<User>(u => u.Username == "myUser"), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<DateTime>())).Returns(userIngredient); var inputFoodModel = new InputFoodModel {Ingredient = "Pannbiff", Measure = 100, Date = DateTime.Now}; var model = testController(x => x.Input(inputFoodModel), userIngredientBusinessLogicMock, userContextMock); Assert.That(model, Is.Not.Null); Assert.That(model.UserIngredients, Is.Not.Null); Assert.That(model.UserIngredients.Any(x => x.Ingredient.Name == "Pannbiff")); Assert.That(model.UserIngredients.Any(x => x.Measure == 100)); userIngredientBusinessLogicMock.VerifyAll(); }
public void shouldReportErrorWhenInvalidDateEnteredOnPage() { var m = new InputFoodModel {Date = new DateTime(1234)}; var userIngredientBusinessLogicMock = new Mock<IUserIngredientBusinessLogic>(MockBehavior.Loose); var userContextMock = new Mock<IUserContext>(MockBehavior.Strict); userIngredientBusinessLogicMock.Setup(x => x.GetUserIngredients(It.IsAny<User>(), It.IsAny<DateTime>())).Throws(new InvalidDateException()); userContextMock.Setup(x => x.User).Returns(new User()); var foodController = new CarbonFitness.App.Web.Controllers.FoodController(userIngredientBusinessLogicMock.Object, new Mock<IRDIProxy>().Object, userContextMock.Object); foodController.Input(m); var errormessage = getErrormessage(foodController, "Date"); Assert.That(errormessage, Is.EqualTo("Invalid date entered. Date should be in format YYYY-MM-DD")); }
private void AssertUserIngredientsExist(MockFactory mockFactory, InputFoodModel model) { Assert.That(model.UserIngredients, Is.Not.Null); Assert.That(model.UserIngredients.Count(), Is.EqualTo(2)); Assert.That(model.UserIngredients.First().Date.ToShortDateString(), Is.EqualTo(DateTime.Now.ToShortDateString())); mockFactory.VerifyAll(); }
public void shouldLoadUserIngredientsAfterAddedIngredients() { var mockFactory = new MockFactory(MockBehavior.Loose); var userContextMock = GetSetuppedUserContextMock(mockFactory); var userIngredientBusinessLogicMock = GetSetuppedUserIngredientBusinessLogicMock(mockFactory); userIngredientBusinessLogicMock.Setup(x => x.AddUserIngredient(It.IsAny<User>(), It.IsAny<string>(), It.IsAny<int>(), It.Is<DateTime>(y => y.ToShortDateString() == DateTime.Now.ToShortDateString()))).Returns(new UserIngredient()); var inputFoodModel = new InputFoodModel {Ingredient = "Pannbiff", Measure = 100, Date = DateTime.Now}; var model = testController(x => x.Input(inputFoodModel), userIngredientBusinessLogicMock, userContextMock); AssertUserIngredientsExist(mockFactory, model); }