public void Post_Existing_Item_Returns_Conflict()
        {
            //Arrange
            DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions();

            var mockData = MockData.LargeShoppingList();

            var testObject = mockData.FirstOrDefault();

            using (var context = new ShoppingListContext(options))
            {
                context.AddRange(mockData);
                context.SaveChanges();
            };

            using (var context = new ShoppingListContext(options))
            {
                IShoppingListRepository mockRepo = new ShoppingListRepository(context);

                var controller = new ShoppingListController(mockRepo);
                controller.ControllerContext             = new ControllerContext();
                controller.ControllerContext.HttpContext = new DefaultHttpContext();

                //Act
                var result = controller.Post(testObject);

                //Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(409, result.StatusCode);
                Assert.AreEqual(result.Value, $"Drink {testObject.Name} already exists in the shopping list.");
            }
        }
        public void Post_To_Empty_List_Successfully_Adds_Item()
        {
            //Arrange
            DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions();

            var expectedObject = new DrinkOrder
            {
                Name     = "Pepsi",
                Quantity = 1
            };

            var expectedResult = new CreatedResult("", expectedObject);

            using (var context = new ShoppingListContext(options))
            {
                IShoppingListRepository mockRepo = new ShoppingListRepository(context);

                var controller = new ShoppingListController(mockRepo);
                controller.ControllerContext             = new ControllerContext();
                controller.ControllerContext.HttpContext = new DefaultHttpContext();

                //Act
                var result = controller.Post(expectedObject) as CreatedResult;

                //Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(201, result.StatusCode);
                //Can't effectively mock HttpRequest, and don't really want to go down that rabbit hole so just check
                //it's not null.
                Assert.IsNotNull(result.Location);
                Assert.AreEqual(result.Value, expectedResult.Value);
            }
        }
Beispiel #3
0
        public void post_should_not_save_to_repository_if_item_null()
        {
            ShoppingListItemCreate request = null;

            var result = _controller.Post(request);

            _repositoryMock.Verify(x => x.Insert(request), Times.Never);
        }