Example #1
0
        public void Should_NotDeleteAnyItem_When_CurrentUserDoesNotHaveItemWithThisId()
        {
            //Arrange
            var allItems = BuildItemsCollection();

            ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems);

            var deleteObject = new Item();

            ItemsRepositoryMock.Setup(c => c.Delete(It.IsAny <Item>()))
            .Callback <Item>((obj) => deleteObject = obj);

            ItemsRepositoryMock.Setup(x => x.SaveChanges());


            //Act
            var response = ItemsDataService.DeleteItem(5, "ab70793b-cec8-4eba-99f3-cbad0b1649d0");


            //Assert
            AssertHelper.AssertAll(
                () => ItemsRepositoryMock.Verify(x => x.Delete(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #2
0
        public void Should_NotDeleteAnyItem_When_UserIdIsNull()
        {
            //Arrange
            var allItems = BuildItemsCollection();

            ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems);

            var deleteObject = new Item();

            ItemsRepositoryMock.Setup(c => c.Delete(It.IsAny <Item>()))
            .Callback <Item>((obj) => deleteObject = obj);

            ItemsRepositoryMock.Setup(x => x.SaveChanges());


            //Act
            var response = ItemsDataService.DeleteItem(1, null);


            //Assert
            AssertHelper.AssertAll(
                () => ItemsRepositoryMock.Verify(x => x.Delete(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #3
0
        public void Should_NotUpdateAnyItemInTheDb_When_AnotherUserHasThisItem()
        {
            //Arrange
            var allItems = BuildItemsCollection();

            ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems);

            ItemsRepositoryMock.Setup(c => c.Update(It.IsAny <Item>()));
            ItemsRepositoryMock.Setup(c => c.SaveChanges());

            var updatedItemDto = new ItemDto()
            {
                Id   = 5,
                Name = "Cake updated",
                NextReplenishmentDate = new DateTime(2020, 10, 10),
                ReplenishmentPeriod   = 4
            };


            //Act
            var result = ItemsDataService.UpdateItem("ab70793b-cec8-4eba-99f3-cbad0b1649d0", updatedItemDto);


            //Assert
            AssertHelper.AssertAll(
                () => ItemsRepositoryMock.Verify(x => x.All(), Times.Once()),
                () => ItemsRepositoryMock.Verify(x => x.Update(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #4
0
        public void Should_CheckForItemDtoNullBeforeAllOtherMethodCalls_When_ItemDtoIsNull()
        {
            //Arrange
            var allItems = BuildItemsCollection();

            ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems);

            ItemsRepositoryMock.Setup(c => c.Update(It.IsAny <Item>()));
            ItemsRepositoryMock.Setup(c => c.SaveChanges());

            var updatedItemDto = new ItemDto()
            {
                Id   = 1,
                Name = "Bread updated",
                NextReplenishmentDate = new DateTime(2020, 10, 10),
                ReplenishmentPeriod   = 4
            };

            //Act
            var result = ItemsDataService.UpdateItem("ab70793b-cec8-4eba-99f3-cbad0b1649d0", null);


            //Assert
            AssertHelper.AssertAll(
                () => ItemsRepositoryMock.Verify(x => x.All(), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.Update(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #5
0
        public void Should_CheckForUserIdEmptyBeforeAllOtherMethodCalls_When_UserIdIsEmpty()
        {
            //Arrange
            var allItems = BuildItemsCollection();

            ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems);

            ItemsRepositoryMock.Setup(c => c.Update(It.IsAny <Item>()));
            ItemsRepositoryMock.Setup(c => c.SaveChanges());

            var updatedItemDto = new ItemDto()
            {
                Id   = 1,
                Name = "Bread updated",
                NextReplenishmentDate = new DateTime(2020, 10, 10),
                ReplenishmentPeriod   = 4
            };

            //Act
            var result = ItemsDataService.UpdateItem(string.Empty, updatedItemDto);


            //Assert
            AssertHelper.AssertAll(
                () => ItemsRepositoryMock.Verify(x => x.All(), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.Update(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #6
0
        public void Should_CheckForUserIdEmptyBeforeOtherMethodCalls_When_UserIdIsEmptyString()
        {
            //Arrange
            var itemDto = BuildFirstItemDtoWithoutId();
            var item    = BuildFirstItemWithoutId();

            MapperMock.Setup(c => c.Map <ItemDto, Item>(itemDto))
            .Returns(item);

            var saveObject = new Item();

            ItemsRepositoryMock.Setup(c => c.Add(It.IsAny <Item>()))
            .Callback <Item>((obj) => saveObject = obj);

            ItemsRepositoryMock.Setup(c => c.SaveChanges());


            //Act
            var result = ItemsDataService.AddItem(string.Empty, itemDto);


            //Assert
            AssertHelper.AssertAll(
                () => MapperMock.Verify(x => x.Map <ItemDto, Item>(It.IsAny <ItemDto>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.Add(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
Example #7
0
        public void Should_AddItemOnce_When_InputParametersAreValid()
        {
            //Arrange
            var itemDto = BuildFirstItemDtoWithoutId();
            var item    = BuildFirstItemWithoutId();

            MapperMock.Setup(c => c.Map <ItemDto, Item>(itemDto))
            .Returns(item);

            var saveObject = new Item();

            ItemsRepositoryMock.Setup(c => c.Add(It.IsAny <Item>()))
            .Callback <Item>((obj) => saveObject = obj);

            ItemsRepositoryMock.Setup(c => c.SaveChanges());


            //Act
            var result = ItemsDataService.AddItem("ab70793b-cec8-4eba-99f3-cbad0b1649d0", itemDto);


            //Assert
            ItemsRepositoryMock.Verify(c => c.Add(item), Times.Once());
        }