コード例 #1
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")
                );
        }
コード例 #2
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");
        }
コード例 #3
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()
                );
        }
コード例 #4
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")
                );
        }
コード例 #5
0
        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());
        }
コード例 #6
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")
                );
        }
コード例 #7
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)
                );
        }
コード例 #8
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()
                );
        }
コード例 #9
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()
                );
        }