public void Put_To_Existing_Item_Updates_Quantity() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); context.Dispose(); }; var expectedObject = new DrinkOrder { Name = "Pepsi", Quantity = 4 }; var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Put(expectedObject) as OkObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.AreEqual(result.Value, expectedResult.Value); //Check context is updated Assert.AreEqual(expectedObject.Quantity, context.shoppingList.FirstOrDefault(x => x.Name == expectedObject.Name).Quantity); } }
public void Put_To_Non_Existing_Item_Returns_Not_Found() { DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); context.Dispose(); }; var expectedObject = new DrinkOrder { Name = "Does Not Exist", Quantity = 4 }; var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Put(expectedObject) as NotFoundObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual(result.Value, "Drink: Does Not Exist not found on the shopping list."); } }