public async Task PostProductTest()
        {
            // Arrange
            var           productsController = this.CreateProductsController();
            ProductCreate product            = new ProductCreate
            {
                Name        = "Product",
                Price       = 275,
                Description = "Description",
                ImgUri      = new Uri("http\\\\web.com\\ghh.png", UriKind.RelativeOrAbsolute)
            };

            Product productExpected = product.ToProduct();

            this.mockDataContext.Setup(m => m.PostProduct(product)).Returns(Task.FromResult(productExpected));
            // Act
            var result = await productsController.PostProduct(product).ConfigureAwait(false);

            var okResult = result as CreatedAtActionResult;

            // Assert
            Assert.NotNull(result);
            Assert.NotNull(okResult);
            Product productOutput = okResult.Value as Product;

            Assert.NotNull(product);
            Assert.Equal(productExpected, productOutput);
            this.mockRepository.VerifyAll();
        }
Пример #2
0
        /// <summary>
        /// Post the product to database.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">product</exception>
        public async Task <Product> PostProduct(ProductCreate product)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }
            var productDb = product.ToProduct();
            await _context.Products.AddAsync(productDb).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(productDb);
        }