public async Task Throw_When_DtoIngredientsIsNull()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Throw_When_DtoIngredientsIsNull));
            var mockCocktailDtoMapper         = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var mockCocktailIngretientService = new Mock <ICocktailIngredientService>();
            var mockIngredientsService        = new Mock <IIngredientService>();
            var mockDateTimeProvider          = new Mock <IDateTimeProvider>();

            string[] ingredients = new string[] { };
            var      cocktailDto = new CocktailDto
            {
                Id          = 1,
                Name        = "TestCocktail",
                Ingredients = ingredients,
            };

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object);

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateCocktailAsync(cocktailDto));
            }
        }
        public async Task Create_Cocktail_Correctly()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(Create_Cocktail_Correctly));
            var mockCocktailDtoMapper         = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var mockCocktailIngretientService = new Mock <ICocktailIngredientService>();
            var mockIngredientsService        = new Mock <IIngredientService>();
            var mockDateTimeProvider          = new Mock <IDateTimeProvider>();
            var ingredient = new Ingredient
            {
                Id   = 1,
                Name = "TestIngredient"
            };

            var ingredientDto = new IngredientDto
            {
                Id   = 1,
                Name = "TestIngredient"
            };

            string[] ingredients = new string[] { "TestIngredient" };

            var cocktailDto = new CocktailDto
            {
                Id          = 1,
                Name        = "TestCocktail",
                Ingredients = ingredients,
            };

            var cocktailIngredient = new CocktailIngredient
            {
                CocktailId   = cocktailDto.Id,
                IngredientId = ingredient.Id
            };

            mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto);
            mockIngredientsService.Setup(x => x.GetIngredientByNameAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(ingredient));
            mockCocktailIngretientService.Setup(x => x.CreateCocktailIngredientAsync(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult(cocktailIngredient));
            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(ingredient);

                await arrangeContext.SaveChangesAsync();
            }

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object);
                var result = await sut.CreateCocktailAsync(cocktailDto);

                Assert.IsInstanceOfType(result, typeof(CocktailDto));
                Assert.AreEqual(1, result.Id);
                Assert.AreEqual("TestCocktail", result.Name);
                Assert.AreEqual("TestIngredient", result.Ingredients.First());
            }
        }
        public async Task ReturnNull_When_NoIngredientsFound()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnNull_When_NoIngredientsFound));
            var mockCocktailDtoMapper         = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var mockCocktailIngretientService = new Mock <ICocktailIngredientService>();
            var mockIngredientsService        = new Mock <IIngredientService>();
            var mockDateTimeProvider          = new Mock <IDateTimeProvider>();
            var ingredient = new Ingredient
            {
                Id   = 1,
                Name = "TestIngredient"
            };

            var ingredientDto = new IngredientDto
            {
                Id   = 1,
                Name = "TestIngredient"
            };

            string[] ingredients = new string[] { "TestIngredient" };

            var cocktailDto = new CocktailDto
            {
                Id          = 1,
                Name        = "TestCocktail",
                Ingredients = ingredients,
            };

            var cocktailIngredient = new CocktailIngredient
            {
                CocktailId   = cocktailDto.Id,
                IngredientId = ingredient.Id
            };

            mockCocktailDtoMapper.Setup(x => x.MapDto(It.IsAny <Cocktail>())).Returns(cocktailDto);
            mockCocktailIngretientService.Setup(x => x.CreateCocktailIngredientAsync(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult(cocktailIngredient));
            using (var arrangeContext = new CocktailMagicianContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(ingredient);

                await arrangeContext.SaveChangesAsync();
            }

            //Act and Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut    = new CocktailService(assertContext, mockCocktailDtoMapper.Object, mockDateTimeProvider.Object, mockCocktailIngretientService.Object, mockIngredientsService.Object);
                var result = await sut.CreateCocktailAsync(cocktailDto);

                Assert.IsNull(result);
            }
        }
Пример #4
0
        public async Task CreateCocktail_WhenParamsAreValid()
        {
            //Arrange
            var mockDateTimeProvider = new Mock <IDateTimeProvider>();
            var mockMapper           = new Mock <ICocktailMapper>();

            mockMapper.Setup(c => c.MapToCocktail(It.IsAny <CocktailDTO>()))
            .Returns <CocktailDTO>(c => new Cocktail
            {
                Name        = c.Name,
                ImageData   = c.ImageData,
                ImageSource = c.ImageSource
            });
            var mockIngMapper             = new Mock <IngredientMapper>();
            var mockBarMapper             = new Mock <IBarMapper>();
            var mockCocktailReviewService = new Mock <ICocktailReviewService>();
            var options = Utils.GetOptions(nameof(CreateCocktail_WhenParamsAreValid));

            var cocktailDTO = new CocktailDTO
            {
                Name        = "New Cocktail",
                ImageData   = new byte[] {},
                ImageSource = "~/Testimage/pqt-ozer-premium.jpg"
            };

            string imageBase64Data = Convert.ToBase64String(cocktailDTO.ImageData);

            cocktailDTO.ImageSource = string.Format("data:image/jpg;base64,{0}", imageBase64Data);

            Utils.GetInMemoryDataBase(options);
            //Act & Assert
            using (var assertContext = new CocktailMagicianContext(options))
            {
                var sut = new CocktailService(mockDateTimeProvider.Object, mockMapper.Object,
                                              mockIngMapper.Object, mockBarMapper.Object, assertContext, mockCocktailReviewService.Object);
                var result = await sut.CreateCocktailAsync(cocktailDTO);

                Assert.IsInstanceOfType(result, typeof(CocktailDTO));
            }
        }