Beispiel #1
0
        public async Task GetByUserIdAsync_WithExistentUserId_ShouldReturnCorrectResult()
        {
            var errorMessagePrefix = "UserAllergenService GetByUserIdAsync() method does not work properly.";

            // Arrange
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userAllergenRepository = new EfRepository <UserAllergen>(context);
            var userAllergenService    = new UserAllergenService(userAllergenRepository);

            await this.SeedDataAsync(context);

            var userId = context.Users.First(x => x.Biography == "User with milk and eggs allergies").Id;

            // Act
            var actualResult = (await userAllergenService
                                .GetByUserIdAsync(userId))
                               .ToList();
            var expectedResult = await context.UserAllergens
                                 .Where(x => x.UserId == userId)
                                 .To <UserAllergenServiceModel>()
                                 .ToListAsync();

            // Assert
            Assert.True(expectedResult.Count == actualResult.Count, errorMessagePrefix + " " + "Collections count mismatch.");

            for (int i = 0; i < actualResult.Count; i++)
            {
                Assert.True(expectedResult[i].UserId == actualResult[i].UserId, errorMessagePrefix + " " + "UserId is not returned properly.");
                Assert.True(expectedResult[i].User.Biography == actualResult[i].User.Biography, errorMessagePrefix + " " + "User Biography is not returned properly.");
                Assert.True(expectedResult[i].AllergenId == actualResult[i].AllergenId, errorMessagePrefix + " " + "AllergenId is not returned properly.");
                Assert.True(expectedResult[i].Allergen.Name == actualResult[i].Allergen.Name, errorMessagePrefix + " " + "Allergen Name is not returned properly.");
            }
        }
Beispiel #2
0
        public async Task GetByUserIdAsync_WithNonExistentUserId_ShouldReturnEmptyCollection()
        {
            var errorMessagePrefix = "UserAllergenService GetByUserIdAsync() method does not work properly.";

            // Arrange
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var userAllergenRepository = new EfRepository <UserAllergen>(context);
            var userAllergenService    = new UserAllergenService(userAllergenRepository);

            await this.SeedDataAsync(context);

            var nonExistentUserId = Guid.NewGuid().ToString();

            // Act
            var actualResult   = (await userAllergenService.GetByUserIdAsync(nonExistentUserId)).Count;
            var expectedResult = 0;

            // Assert
            Assert.True(expectedResult == actualResult, errorMessagePrefix + " " + "Collections is not empty.");
        }