public void WhenPut_ValidItem_ReturnsOk() { // Arrange int itemId = 2; var newItem = new BudgetItem { Id = itemId, Notes = "Office Lunch", BudgetLocationId = 2, BudgetSubCategoryId = 2, Amount = 15, DateOccured = DateTime.Now, IsExpense = true, IsFixed = false }; IEnumerable<BudgetItem> fakeItems = GetFakeItems(); _itemRepository.Setup(x => x.GetItem(itemId)).Returns(fakeItems.FirstOrDefault(p => p.Id == itemId)); _itemRepository.Setup(x => x.UpdateItem(newItem)).Returns(newItem); var controller = new ItemsController(_itemRepository.Object); // Act IHttpActionResult actionResult = controller.Put(2, newItem); // Assert Assert.IsNotNull(actionResult); var item = actionResult as OkNegotiatedContentResult<BudgetItem>; Assert.IsNotNull(item); Assert.AreEqual(item.Content.BudgetLocationId, 2); }
public void WhenPut_InValidItem_CreatesNewItem() { // Arrange var fakeId = 100; var newItem = new BudgetItem { Id = fakeId, Notes = "Office Lunch", BudgetLocationId = 2, BudgetSubCategoryId = 2, Amount = 15, DateOccured = DateTime.Now, IsExpense = true, IsFixed = false }; IEnumerable<BudgetItem> fakeItems = GetFakeItems(); _itemRepository.Setup(x => x.GetItem(fakeId)).Returns(fakeItems.FirstOrDefault(p => p.Id == fakeId)); var controller = new ItemsController(_itemRepository.Object); // Act IHttpActionResult actionResult = controller.Put(fakeId, newItem); // Assert Assert.IsNotNull(actionResult); var createdResult = actionResult as CreatedAtRouteNegotiatedContentResult<BudgetItem>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual("DefaultApi", createdResult.RouteName); Assert.AreEqual(fakeId, createdResult.RouteValues["id"]); }