예제 #1
0
        public void Should_ReturnAnErrorResponse_When_ItemRepositoryThrowsAnException()
        {
            //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())
            .Throws(new Exception());


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


            //Assert
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("An error occurred while creating item")
                );
        }
예제 #2
0
        public void Should_ReturnASuccessResponse_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
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeTrue(),
                () => result.ErrorMessage.Should().BeNull()
                );
        }
예제 #3
0
        public void Should_AddItem_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
            AssertHelper.AssertAll(
                () => saveObject.Id.Should().Be(0),
                () => saveObject.Name.Should().Be("Bread"),
                () => saveObject.NextReplenishmentDate.Should().BeSameDateAs(new DateTime(2020, 10, 06)),
                () => saveObject.ReplenishmentPeriod.Should().Be(1)
                );
        }
예제 #4
0
        public void Should_CallRepositorySaveChangesAfterUpdateMethod_When_InputParametersAreValid()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

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


            var sequence = new MockSequence();

            ItemsRepositoryMock
            .InSequence(sequence)
            .Setup(c => c.Update(It.Is <Item>(x =>
                                              x.HasTheSameProperties("ab70793b-cec8-4eba-99f3-cbad0b1649d0", updatedItemDto))));

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

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

            //Assert
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeTrue(),
                () => result.ErrorMessage.Should().BeNull()
                );
        }
예제 #5
0
        public void Should_ReturnResponseWithCorrectItemName_When_OneItemNameOfThisUserContainSearchWord()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var filteredItem  = BuildFirstItem();
            var filteredItems = new List <Item>()
            {
                filteredItem
            };

            var filteredItemNameDto  = BuildFirstItemNameDto();
            var filteredItemNameDtos = new List <ItemNameDto>()
            {
                filteredItemNameDto
            };

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemNameDtos.AsQueryable());

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

            //Assert
            result.Data.First().Name.Should().Be("Bread");
        }
예제 #6
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())
                );
        }
예제 #7
0
        public void Should_UpdateItem_When_InputParametersAreValid()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var saveObject = new Item();

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

            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", updatedItemDto);


            //Assert
            AssertHelper.AssertAll(
                () => saveObject.Id.Should().Be(1),
                () => saveObject.Name.Should().Be("Bread updated"),
                () => saveObject.NextReplenishmentDate.Should().BeSameDateAs(new DateTime(2020, 10, 10)),
                () => saveObject.ReplenishmentPeriod.Should().Be(4)
                );
        }
예제 #8
0
        public void Should_ReturnErrorResponse_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(
                () => response.IsSuccess.Should().BeFalse(),
                () => response.ErrorMessage.Should().Be("An error occurred while deleting item")
                );
        }
예제 #9
0
        public void Should_ReturnAnErrorResponse_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(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("An error occurred while creating item")
                );
        }
예제 #10
0
        public void Should_ReturnErrorResponse_When_NoItemsExist()
        {
            //Arrange
            var allItems = new List <Item>().AsQueryable();

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

            var filteredItems    = new List <Item>();
            var filteredItemDtos = new List <ItemDto>();

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemDtos.AsQueryable());


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


            //Assert
            AssertHelper.AssertAll(
                () => result.Data.Should().BeNull(),
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("Current user does not have item with id 2")
                );
        }
예제 #11
0
        public void Should_ReturnErrorResponse_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(
                () => response.IsSuccess.Should().BeFalse(),
                () => response.ErrorMessage.Should().Be("Current user does not have item with id 5")
                );
        }
예제 #12
0
        public void Should_ReturnErrorResponse_When_CurrentUserDoesNotHaveItemWithThisId()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var filteredItems    = new List <Item>();
            var filteredItemDtos = new List <ItemDto>();

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemDtos.AsQueryable());


            //Act
            var result = ItemsDataService.GetItem(2, "925912b0-c59c-4e1b-971a-06e8abab7848");


            //Assert
            AssertHelper.AssertAll(
                () => result.Data.Should().BeNull(),
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("Current user does not have item with id 2")
                );
        }
예제 #13
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())
                );
        }
예제 #14
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())
                );
        }
예제 #15
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())
                );
        }
예제 #16
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())
                );
        }
예제 #17
0
        public void Should_ReturnErrorResponse_When_RepositorySaveChangesThrowsAnException()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

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

            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", updatedItemDto);


            //Assert
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("An error occurred while updating item")
                );
        }
예제 #18
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())
                );
        }
예제 #19
0
        public void Should_ReturnErrorResponse_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(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("Current user does not have item with id 5")
                );
        }
예제 #20
0
        public void Should_ReturnErrorResponse_When_AnExceptionOccursInProjectToMethod()
        {
            //Arrange
            var allItems = new List <Item>().AsQueryable();

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

            var filteredItems = new List <Item>();

            MapperMock.Setup(c => c.ProjectTo(
                                 It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                                 null,
                                 It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0)))
            .Throws(new Exception());

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

            //Assert
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("An error occured while getting items names."),
                () => result.Data.Should().BeNull()
                );
        }
예제 #21
0
        public void Should_CallDeletemethodWithCorrectItem_When_InputParametersAreValid()
        {
            //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, "ab70793b-cec8-4eba-99f3-cbad0b1649d0");


            //Assert
            AssertHelper.AssertAll(
                () => deleteObject.Id.Should().Be(1),
                () => deleteObject.Name.Should().Be("Bread"),
                () => deleteObject.NextReplenishmentDate.Should().BeSameDateAs(new DateTime(2020, 10, 06)),
                () => deleteObject.ReplenishmentPeriod.Should().Be(1),
                () => deleteObject.UserId.Should().Be("ab70793b-cec8-4eba-99f3-cbad0b1649d0")
                );
        }
        protected void InitializeMocksWithEmptyCollection()
        {
            var allItems = new List <Item>().AsQueryable();

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

            var filteredItems        = new List <Item>();
            var filteredItemNameDtos = new List <ItemNameDto>();

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemNameDtos.AsQueryable());
        }
예제 #23
0
        public void Should_ReturnSuccessResponse_When_InputParametersAreValid()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

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


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


            //Assert
            AssertHelper.AssertAll(
                () => response.IsSuccess.Should().BeTrue(),
                () => response.ErrorMessage.Should().BeNull()
                );
        }
예제 #24
0
        public void Should_ReturnErrorResponse_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());


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


            //Assert
            AssertHelper.AssertAll(
                () => result.IsSuccess.Should().BeFalse(),
                () => result.ErrorMessage.Should().Be("An error occurred while updating item")
                );
        }
예제 #25
0
        public void Should_ReturnResponseWithCorrectItemsNamesOfAllItemsOfThisUser_When_SearchWordIsEmptyString()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var firstFilteredItem  = BuildFirstItem();
            var secondFilteredItem = BuildSecondItem();
            var thirdFilteredItem  = BuildThirdItem();
            var filteredItems      = new List <Item>()
            {
                firstFilteredItem, secondFilteredItem, thirdFilteredItem
            };

            var firstFilteredItemNameDto  = BuildFirstItemNameDto();
            var secondFilteredItemNameDto = BuildSecondItemNameDto();
            var thirdFilteredItemNameDto  = BuildThirdItemNameDto();
            var filteredItemNameDtos      = new List <ItemNameDto>()
            {
                firstFilteredItemNameDto, secondFilteredItemNameDto, thirdFilteredItemNameDto
            };

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemNameDtos.AsQueryable());

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

            //Assert
            AssertHelper.AssertAll(
                () => result.Data.First().Name.Should().Be("Bread"),
                () => result.Data.Skip(1).First().Name.Should().Be("Cheese"),
                () => result.Data.Skip(2).First().Name.Should().Be("Biscuits")
                );
        }
예제 #26
0
        public void Should_ReturnResponseWithCorrectItem_When_CurrentUserHasItemWithThisId()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var filteredItem  = BuildSecondItem();
            var filteredItems = new List <Item>()
            {
                filteredItem
            };

            var filteredItemDto  = BuildSecondItemDto();
            var filteredItemDtos = new List <ItemDto>()
            {
                filteredItemDto
            };

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemDtos.AsQueryable());


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


            //Assert
            AssertHelper.AssertAll(
                () => result.Data.Id.Should().Be(2),
                () => result.Data.Name.Should().Be("Cheese"),
                () => result.Data.NextReplenishmentDate.Should().BeSameDateAs(new DateTime(2020, 10, 08)),
                () => result.Data.ReplenishmentPeriod.Should().Be(2)
                );
        }
예제 #27
0
        public void Should_ReturnSuccessResponseWithItem_When_CurrentUserHasItemWithThisId()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var filteredItem  = BuildSecondItem();
            var filteredItems = new List <Item>()
            {
                filteredItem
            };

            var filteredItemDto  = BuildSecondItemDto();
            var filteredItemDtos = new List <ItemDto>()
            {
                filteredItemDto
            };

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemDtos.AsQueryable());


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


            //Assert
            AssertHelper.AssertAll(
                () => result.Data.Should().NotBeNull(),
                () => result.IsSuccess.Should().BeTrue(),
                () => result.ErrorMessage.Should().BeNull()
                );
        }
예제 #28
0
        public void Should_ReturnSuccessResponseWithTwoEntries_When_TwoItemsNamesOfThisUserContainSearchWord()
        {
            //Arrange
            var allItems = BuildItemsCollection();

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

            var firstFilteredItem  = BuildFirstItem();
            var secondFilteredItem = BuildThirdItem();
            var filteredItems      = new List <Item>()
            {
                firstFilteredItem, secondFilteredItem
            };

            var firstFilteredItemNameDto  = BuildFirstItemNameDto();
            var secondFilteredItemNameDto = BuildThirdItemNameDto();
            var filteredItemNameDtos      = new List <ItemNameDto>()
            {
                firstFilteredItemNameDto, secondFilteredItemNameDto
            };

            MapperMock
            .Setup(c => c.ProjectTo(
                       It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)),
                       null,
                       It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0)))
            .Returns(filteredItemNameDtos.AsQueryable());

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

            //Assert
            AssertHelper.AssertAll(
                () => result.Data.Count().Should().Be(2),
                () => result.IsSuccess.Should().BeTrue(),
                () => result.ErrorMessage.Should().BeNull()
                );
        }
예제 #29
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());
        }