Пример #1
0
 public ProductsController(IProductsService productsService, ILogger <ProductsController> logger)
 {
     _productsService        = productsService;
     _productConverter       = new ProductDoDtoConverter();
     _productOptionConverter = new ProductOptionDoDtoConverter();
     _logger = logger;
 }
        public void PostProductTest()
        {
            ProductDto product = new ProductDto {
                Name = "Samsung Galaxy", Description = "mobile", Price = 199.99M, DeliveryPrice = 11.11M
            };
            var     productsService = new Mock <IProductsService>();
            Guid    productId       = Guid.NewGuid();
            Product model           = new Product {
                Id = productId, Name = "Samsung Galaxy", Description = "mobile", Price = 199.99M, DeliveryPrice = 11.11M
            };

            productsService
            .Setup(repo => repo.CreateProduct(new ProductDoDtoConverter().ToDO(product)))
            .Returns(model);
            ProductsController productsController = new ProductsController(productsService.Object, Mock.Of <ILogger <ProductsController> >());

            ActionResult result = productsController.PostProduct(product);

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

            Assert.IsInstanceOfType(createdAtActionResult.Value, typeof(ProductDto));
            ProductDto expectedProduct = new ProductDoDtoConverter().FromDO(model);

            Assert.AreEqual(expectedProduct, (ProductDto)createdAtActionResult.Value);
            object obj;
            bool   contains = createdAtActionResult.RouteValues.TryGetValue("id", out obj);

            Assert.AreEqual(true, contains);
            Assert.AreEqual(productId, obj);
        }
        public void GetProductTest()
        {
            Guid    productId = Guid.NewGuid();
            Product product   = new Product {
                Id = productId, Name = "Samsung Galaxy", Description = "mobile", Price = 199.99M, DeliveryPrice = 11.11M
            };
            var productsService = new Mock <IProductsService>();

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

            ActionResult result = productsController.GetProduct(productId);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
            OkObjectResult okObjectResult = (OkObjectResult)result;

            Assert.IsInstanceOfType(okObjectResult.Value, typeof(ProductDto));
            ProductDto expectedProduct = new ProductDoDtoConverter().FromDO(product);

            Assert.AreEqual(expectedProduct, (ProductDto)okObjectResult.Value);
        }