public void GetItemList_GivenManyItems_ShouldReturnListOfThoseItems() { //---------------Set up test pack------------------- var testItemList = SetUpItemDataContext(); var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context, testItemList); var repo = new ItemRepository(context); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = repo.GetItemList(); //---------------Test Result ----------------------- Assert.AreEqual(testItemList, result); CollectionAssert.AreEqual(testItemList, result); }
public void GetItemList_GivenOneItem_ShouldReturnThatItem() { var testItem = new ItemDto() { Id = 1, Description = "Description one", ItemName = "Ball" }; var testItemList = new List<ItemDto>() { testItem }; var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context, testItemList); //---------------Set up test pack------------------- var repo = new ItemRepository(context); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = repo.GetItemList(); //---------------Test Result ----------------------- Assert.AreEqual(testItemList, result); }
public void Create_Post_GivenNoItem_ShouldReturnThrowAndTheSameView() { //---------------Set up test pack------------------- var repo = new ItemRepository(Substitute.For<ILendingLibraryContext>()); var controller = new ItemController(repo); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var viewResult = controller.Create(null); //---------------Test Result ----------------------- Assert.IsNotNull(viewResult); }
public void Add_GivenNewItem_ShouldAddToList() { //---------------Set up test pack------------------- var newItem = new ItemDto() { Id = 4, Description = "Description four", ItemName = "AnotherItemName" }; var itemsList = new List<ItemDto>(); var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context,itemsList); var repo = new ItemRepository(context); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Add(newItem); //var result = repo.GetItemList(); var result = context.Items.Where(x => x.Id == newItem.Id); Assert.IsNotNull(result); }
public void Update_GivenItemIdThatDoesNotExist_ShouldAddToItemList() { var items = SetUpItemDataContext(); //---------------Set up test pack------------------- var itemToUpdate = new ItemDto() { Id = 7, Description = "ItemDTO I'm going to update", ItemName = "ItemToUpdate" }; var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context, items); var repo = new ItemRepository(context); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Update(itemToUpdate); var addedItem = repo.GetItemById(itemToUpdate.Id); //---------------Test Result ----------------------- Assert.AreEqual(itemToUpdate, addedItem); Assert.IsNotNull(addedItem); }
public void Update_GivenItemId_ShouldUpdateTheItem() { var items = SetUpItemDataContext(); //---------------Set up test pack------------------- var itemToUpdate = items[0]; var context = Substitute.For<ILendingLibraryContext>(); var repo = new ItemRepository(context); SetContextWithDtos(context, items); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- //---------------Test Result ----------------------- Assert.IsTrue(repo.GetItemList().Contains(itemToUpdate)); itemToUpdate.ItemName = "Book One v2.0"; repo.Update(itemToUpdate); var updatedItem = context.Items.FirstOrDefault(i => i.Id == itemToUpdate.Id); Assert.IsNotNull(updatedItem); Assert.AreEqual(itemToUpdate, updatedItem); }
public void Delete_GivenItemIdDoesNotExist_ShouldThrow() { //---------------Set up test pack------------------- var context = Substitute.For<ILendingLibraryContext>(); var repo = new ItemRepository(context); SetContextWithDtos(context, new List<ItemDto>()); const string expectedMessage = "Item has already been removed"; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = Assert.Throws<ApplicationException>(() => repo.Delete(9)); //---------------Test Result ----------------------- Assert.AreEqual(expectedMessage, result.Message); }
public void Delete_GivenItemId_ShouldDeleteThatItem() { //---------------Set up test pack------------------- var items = SetUpItemDataContext(); var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context, items); var repo = new ItemRepository(context); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- //---------------Test Result ----------------------- Assert.IsTrue(repo.GetItemList().Contains(items[1])); repo.Delete(items[1].Id); var newItem = context.Items.FirstOrDefault(x => x.Id != items[1].Id); Assert.IsNotNull(newItem); }
public void Add_GivenNullItem_ShouldNotAddToListAndThrow() { //---------------Set up test pack------------------- var itemList = new List<ItemDto> { null }; var context = Substitute.For<ILendingLibraryContext>(); SetContextWithDtos(context, itemList); var repo = new ItemRepository(context); const string message = "Null item cannot be added"; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = Assert.Throws<ApplicationException>(() => repo.Add((ItemDto) null)); //---------------Test Result ----------------------- Assert.AreEqual(message, result.Message); }