Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [TestMethod]
- public async Task CreateCocktailCorrectly()
- {
- //Arrange
- var options = TestUtilities.GetOptions(nameof(CreateCocktailCorrectly));
- var ingredientServiceMock = new Mock<ICocktailIngredientService>();
- var cocktailMapperToDTOMock = new Mock<IDTOServiceMapper<CocktailDTO, Cocktail>>();
- var cocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, CocktailDTO>>();
- var commentMapperToDTOMock = new Mock<IDTOServiceMapper<CommentDTO, CocktailComment>>();
- var commentMapperMock = new Mock<IDTOServiceMapper<CocktailComment, CommentDTO>>();
- var addCocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, AddCocktailDTO>>();
- var cocktailRatingToDTOMock = new Mock<IDTOServiceMapper<RatingDTO, CocktailRating>>();
- var cocktailDTOMock = new Mock<CocktailDTO>();
- var cocktailIngredientDTOMock = new Mock<List<CocktailIngredientDTO>>();
- var cocktailMock = new Mock<Cocktail>();
- cocktailDTOMock.Object.Ingredients = cocktailIngredientDTOMock.Object;
- //Act
- cocktailMapperToDTOMock.Setup(m => m.MapFrom(It.IsAny<CocktailDTO>())).Returns(cocktailMock.Object);
- using (var actContext = new IriOnCocktailServiceDbContext(options))
- {
- var sut = new CocktailService(actContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object);
- await sut.CreateCocktail(cocktailDTOMock.Object);
- }
- using (var assertContext = new IriOnCocktailServiceDbContext(options))
- {
- var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object);
- Assert.AreEqual(1, assertContext.Cocktails.Count());
- }
- }
- public async Task<CocktailDTO> CreateCocktail(CocktailDTO cocktailDTO)
- {
- var cocktail = this.cocktailMapper.MapFrom(cocktailDTO);
- await this.context.Cocktails.AddAsync(cocktail);
- await this.context.SaveChangesAsync();
- cocktailDTO.Ingredients.ForEach(cdto => cdto.CocktailId = cocktail.Id);
- await cocktailIngredientService.CreateCocktailIngredient(cocktailDTO.Ingredients);
- return cocktailDTO;
- }
- public async Task<List<CocktailIngredientDTO>> CreateCocktailIngredient(List<CocktailIngredientDTO> cocktailIngredientDTOs)
- {
- foreach (var cocktailIngredientDTO in cocktailIngredientDTOs)
- {
- var cocktailIngredient = this.cocktailIngredientMapper.MapFrom(cocktailIngredientDTO);
- await this.context.CocktailIngredients.AddAsync(cocktailIngredient);
- }
- await this.context.SaveChangesAsync();
- return cocktailIngredientDTOs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement