Ejemplo n.º 1
0
        public async Task GetAllItemsForUser_ShouldReturnEmptyList_IfUserCannotBeMatchedOrDataDoesntExist()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                IEnumerable <UserItemDto> userItems = await userItemQueries.GetAllItemsForUser(1);

                // Assert
                Assert.Empty(userItems);
            }
        }
Ejemplo n.º 2
0
        public async Task GetAllItemsForUser_ShouldReturnListOfUserItemsWithCorrectCounts_IfDataCanBeMatched()
        {
            using (ApplicationDbContext context = ApplicationDbContextHelper.GetContext())
            {
                // Arrange
                int    userId   = 1;
                string userName = "******";
                context.Add(new User {
                    Id = userId, Name = userName
                });

                int    numberOfSwords = 46;
                int    swordId        = 1;
                string swordName      = "Longsword";
                context.Add(new Item {
                    Id = swordId, Name = swordName
                });

                int    numberOfBatteries = 3;
                int    batteryId         = 2;
                string batteryName       = "Large Battery";
                context.Add(new Item {
                    Id = batteryId, Name = batteryName
                });

                for (int i = 0; i < numberOfSwords; i++)
                {
                    context.Add(new UserItem {
                        ItemId = swordId, UserId = userId
                    });
                }
                for (int i = 0; i < numberOfBatteries; i++)
                {
                    context.Add(new UserItem {
                        ItemId = batteryId, UserId = userId
                    });
                }

                context.SaveChanges();

                IMapper mapper          = GetAutoMapperMapper();
                var     userItemQueries = new UserItemQueries(context, mapper);

                // Act
                IEnumerable <UserItemDto> actualUserItems = await userItemQueries.GetAllItemsForUser(userId);

                // Assert
                var expectedUserItems = new List <UserItemDto>
                {
                    new UserItemDto
                    {
                        UserId    = userId,
                        UserName  = userName,
                        ItemCount = numberOfSwords,
                        ItemId    = swordId,
                        ItemName  = swordName
                    },
                    new UserItemDto
                    {
                        UserId    = userId,
                        UserName  = userName,
                        ItemCount = numberOfBatteries,
                        ItemId    = batteryId,
                        ItemName  = batteryName
                    }
                };
                Assert.Equal
                (
                    JsonConvert.SerializeObject(expectedUserItems),
                    JsonConvert.SerializeObject(actualUserItems)
                );
            }
        }