Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1.  
  2. [TestMethod]
  3. public async Task CreateCocktailCorrectly()
  4. {
  5. //Arrange
  6. var options = TestUtilities.GetOptions(nameof(CreateCocktailCorrectly));
  7.  
  8. var ingredientServiceMock = new Mock<ICocktailIngredientService>();
  9. var cocktailMapperToDTOMock = new Mock<IDTOServiceMapper<CocktailDTO, Cocktail>>();
  10. var cocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, CocktailDTO>>();
  11. var commentMapperToDTOMock = new Mock<IDTOServiceMapper<CommentDTO, CocktailComment>>();
  12. var commentMapperMock = new Mock<IDTOServiceMapper<CocktailComment, CommentDTO>>();
  13. var addCocktailMapperMock = new Mock<IDTOServiceMapper<Cocktail, AddCocktailDTO>>();
  14. var cocktailRatingToDTOMock = new Mock<IDTOServiceMapper<RatingDTO, CocktailRating>>();
  15.  
  16. var cocktailDTOMock = new Mock<CocktailDTO>();
  17. var cocktailIngredientDTOMock = new Mock<List<CocktailIngredientDTO>>();
  18. var cocktailMock = new Mock<Cocktail>();
  19. cocktailDTOMock.Object.Ingredients = cocktailIngredientDTOMock.Object;
  20. //Act
  21. cocktailMapperToDTOMock.Setup(m => m.MapFrom(It.IsAny<CocktailDTO>())).Returns(cocktailMock.Object);
  22.  
  23. using (var actContext = new IriOnCocktailServiceDbContext(options))
  24. {
  25. var sut = new CocktailService(actContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object);
  26.  
  27. await sut.CreateCocktail(cocktailDTOMock.Object);
  28. }
  29.  
  30. using (var assertContext = new IriOnCocktailServiceDbContext(options))
  31. {
  32. var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object);
  33.  
  34. Assert.AreEqual(1, assertContext.Cocktails.Count());
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. public async Task<CocktailDTO> CreateCocktail(CocktailDTO cocktailDTO)
  43. {
  44. var cocktail = this.cocktailMapper.MapFrom(cocktailDTO);
  45.  
  46. await this.context.Cocktails.AddAsync(cocktail);
  47. await this.context.SaveChangesAsync();
  48.  
  49. cocktailDTO.Ingredients.ForEach(cdto => cdto.CocktailId = cocktail.Id);
  50. await cocktailIngredientService.CreateCocktailIngredient(cocktailDTO.Ingredients);
  51.  
  52. return cocktailDTO;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. public async Task<List<CocktailIngredientDTO>> CreateCocktailIngredient(List<CocktailIngredientDTO> cocktailIngredientDTOs)
  60. {
  61. foreach (var cocktailIngredientDTO in cocktailIngredientDTOs)
  62. {
  63. var cocktailIngredient = this.cocktailIngredientMapper.MapFrom(cocktailIngredientDTO);
  64.  
  65. await this.context.CocktailIngredients.AddAsync(cocktailIngredient);
  66. }
  67. await this.context.SaveChangesAsync();
  68.  
  69. return cocktailIngredientDTOs;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement