Exemplo n.º 1
0
        public async Task ReturnNull_When_NoRatingsAreFound()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnNull_When_NoRatingsAreFound));
            var mockDateTimeProvider = new Mock <IDateTimeProvider>();
            var mapper = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >();

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new CocktailRatingService(assertContext, mockDateTimeProvider.Object, mapper.Object);
                var result = await sut.GetAllRatingsAsync(1);

                Assert.IsNull(result);
            }
        }
        public async Task ReturnInstanceOfCollectionCocktailRatingDtos()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnInstanceOfCollectionCocktailRatingDtos));

            var mapperMock = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >();

            var cocktailId = Guid.NewGuid();
            var userId     = Guid.NewGuid();
            var userIdTwo  = Guid.NewGuid();
            var date1      = new DateTime(2017, 1, 18);
            var date2      = new DateTime(2017, 1, 20);

            var cocktail = new Cocktail
            {
                Id   = cocktailId,
                Name = "testName"
            };

            var user = new User
            {
                Id       = userId,
                UserName = "******"
            };

            var entity = new CocktailRating
            {
                CocktailId = cocktailId,
                UserId     = userId,
                Value      = 2,
                CreatedOn  = date1,
            };

            var entityTwo = new CocktailRating
            {
                CocktailId = cocktailId,
                UserId     = userIdTwo,
                Value      = 2,
                CreatedOn  = date2,
            };

            var list = new List <CocktailRatingDto>()
            {
                new CocktailRatingDto {
                    CocktailId = cocktailId, UserId = userId, Value = 2
                },
                new CocktailRatingDto {
                    CocktailId = cocktailId, UserId = userIdTwo, Value = 2
                },
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <CocktailRating> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                //await arrangeContext.Cocktails.AddAsync(cocktail);
                //await arrangeContext.Users.AddAsync(user);
                await arrangeContext.CocktailRatings.AddAsync(entity);

                await arrangeContext.CocktailRatings.AddAsync(entityTwo);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new CocktailRatingService(assertContext, mapperMock.Object);

                var result = await sut.GetAllRatingsAsync(cocktailId);

                Assert.IsInstanceOfType(result, typeof(ICollection <CocktailRatingDto>));
                Assert.AreEqual(2, result.Count());
                Assert.AreEqual(entity.Value, result.First().Value);
                Assert.AreEqual(entity.UserId, result.First().UserId);
                Assert.AreEqual(entityTwo.Value, result.Last().Value);
                Assert.AreEqual(entityTwo.UserId, result.Last().UserId);
            }
        }
Exemplo n.º 3
0
        public async Task ReturnCorrectEntity_When_ParamsAreValid()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCorrectEntity_When_ParamsAreValid));
            var mockDateTimeProvider = new Mock <IDateTimeProvider>();
            var mapper = new Mock <IDtoMapper <CocktailRating, CocktailRatingDto> >();

            var user1 = new User {
                Id = 1
            };
            var user2 = new User {
                Id = 2
            };
            var cocktail = new Cocktail {
                Id = 1
            };

            var rating1 = new CocktailRating
            {
                Id         = 1,
                UserId     = 1,
                CocktailId = 1,
                Value      = 5,
            };
            var rating2 = new CocktailRating
            {
                Id         = 2,
                UserId     = 2,
                CocktailId = 1,
                Value      = 3,
            };

            var list = new List <CocktailRatingDto>()
            {
                new CocktailRatingDto
                {
                    Id         = 1,
                    UserId     = 1,
                    CocktailId = 1,
                    Value      = 5,
                },

                new CocktailRatingDto
                {
                    Id         = 2,
                    UserId     = 2,
                    CocktailId = 1,
                    Value      = 3,
                }
            };

            mapper.Setup(x => x.MapDto(It.IsAny <ICollection <CocktailRating> >())).Returns(list);
            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.Users.AddAsync(user1);

                await arrangeContext.Users.AddAsync(user2);

                await arrangeContext.CocktailRatings.AddAsync(rating1);

                await arrangeContext.CocktailRatings.AddAsync(rating2);

                await arrangeContext.SaveChangesAsync();
            }
            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new CocktailRatingService(assertContext, mockDateTimeProvider.Object, mapper.Object);
                var result = await sut.GetAllRatingsAsync(1);

                Assert.AreEqual(2, result.Count);
                Assert.AreEqual(1, result.First().Id);
                Assert.AreEqual(1, result.First().UserId);
                Assert.AreEqual(1, result.First().CocktailId);
                Assert.AreEqual(5, result.First().Value);
                Assert.AreEqual(2, result.Last().Id);
                Assert.AreEqual(2, result.Last().UserId);
                Assert.AreEqual(1, result.Last().CocktailId);
                Assert.AreEqual(3, result.Last().Value);
            }
        }