public ActionResult CreateList(FormCollection collection) { try { var name = collection["listname"]; if (name == null || name == "") { throw new Exception("Name cannot be null."); } var sessionUserId = Session["UserId"]; int userid = int.Parse(sessionUserId.ToString()); ShoppingListDto list = new ShoppingListDto { Name = name, Path = "somerandomPath", ListAccessTypeId = 1, //Default owner when creating UserId = userid, ChosenSortingId = null }; ShoppingListService listService = new ShoppingListService(); listService.Create(list); TempData["SuccessMessage"] = "You successfully created a new shopping list!"; return(RedirectToAction("Lists")); } catch (Exception) { TempData["ErrorMessage"] = "Something went wrong. Make sure to have entered a valid list name. Try again!"; return(RedirectToAction("Lists")); } }
public void TestCreateShouldAssignNextUniqueTitle() { var mockShoppingListRepository = new Mock <IShoppingListRepository>(MockBehavior.Strict); mockShoppingListRepository.Setup(slr => slr.FindByPartialTitleMatch("Shopping List #", userIds[0])).Returns( new List <ShoppingList>() { new ShoppingList { Id = 1231, Title = "Shopping List #Z", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1231, Title = "Shopping List #A", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1231, Title = "Shopping List #3", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1232, Title = "Shopping List #2", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1233, Title = "Shopping List #1", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1233, Title = "Shopping List #0", CreatorId = userIds[0], CreatedDate = DateTime.Now }, new ShoppingList { Id = 1233, Title = "Shopping List #10", CreatorId = userIds[0], CreatedDate = DateTime.Now } } ); mockShoppingListRepository.Setup(slr => slr.Create(It.IsAny <ShoppingList>())); container.StartMocking <IShoppingListRepository>(() => mockShoppingListRepository.Object); var mockShoppingListPermissionRepository = new Mock <IShoppingListPermissionRepository>(); container.StartMocking <IShoppingListPermissionRepository>(() => mockShoppingListPermissionRepository.Object); service = container.GetInstance <ShoppingListService>(); var shoppingList = service.Create(userIds[0]); Assert.AreEqual("Shopping List #11", shoppingList.Title); }