Пример #1
0
 public ProductsController(IProductsService productsService, ILogger <ProductsController> logger)
 {
     _productsService        = productsService;
     _productConverter       = new ProductDoDtoConverter();
     _productOptionConverter = new ProductOptionDoDtoConverter();
     _logger = logger;
 }
        public void PostProductOptionTest()
        {
            Guid             productId     = Guid.NewGuid();
            ProductOptionDto productOption = new ProductOptionDto {
                Name = "OptionA", Description = "desc", ProductId = productId
            };
            ProductOption model = new ProductOption {
                Id = Guid.NewGuid(), Name = "OptionA", Description = "desc", ProductId = productId
            };
            var productsService = new Mock <IProductsService>();

            productsService
            .Setup(repo => repo.CreateProductOption(productId, new ProductOptionDoDtoConverter().ToDO(productOption)))
            .Returns(model);
            ProductsController productsController = new ProductsController(productsService.Object, Mock.Of <ILogger <ProductsController> >());

            ActionResult result = productsController.PostProductOption(productId, productOption);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult));
            CreatedAtActionResult createdAtActionResult = (CreatedAtActionResult)result;

            Assert.IsInstanceOfType(createdAtActionResult.Value, typeof(ProductOptionDto));
            ProductOptionDto expectedProduct = new ProductOptionDoDtoConverter().FromDO(model);

            Assert.AreEqual(expectedProduct, (ProductOptionDto)createdAtActionResult.Value);
            object productIdActual;
            bool   contains = createdAtActionResult.RouteValues.TryGetValue("productId", out productIdActual);

            Assert.AreEqual(true, contains);
            Assert.AreEqual(productId, productIdActual);
            object productOptionId;

            contains = createdAtActionResult.RouteValues.TryGetValue("productOptionId", out productOptionId);
            Assert.AreEqual(true, contains);
            Assert.AreEqual(model.Id, productOptionId);
        }
        public void GetProductOptionTest()
        {
            Guid          productId       = Guid.NewGuid();
            Guid          productOptionId = Guid.NewGuid();
            ProductOption productOption   = new ProductOption {
                Id = productOptionId, ProductId = productId, Name = "OptionA", Description = "with keyboard"
            };
            var productsService = new Mock <IProductsService>();

            productsService
            .Setup(repo => repo.GetProductOption(productId, productOptionId))
            .Returns(productOption);
            ProductsController productsController = new ProductsController(productsService.Object, Mock.Of <ILogger <ProductsController> >());

            ActionResult result = productsController.GetProductOption(productId, productOptionId);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            OkObjectResult   okObjectResult           = (OkObjectResult)result;
            ProductOptionDto productOptionDtoExpected = new ProductOptionDoDtoConverter().FromDO(productOption);

            Assert.IsInstanceOfType(okObjectResult.Value, typeof(ProductOptionDto));
            Assert.AreEqual(productOptionDtoExpected, (ProductOptionDto)okObjectResult.Value);
        }