コード例 #1
0
        public void SaveProduct_ThrowsException()
        {
            // Arrange
            var product = new Product
            {
                Id          = 1,
                Code        = "Code",
                Description = "Description",
                Price       = 10
            };

            _productRepository.Setup(x => x.AddProduct(product)).Throws(new Exception("An error occurred while adding new Product"));
            var productCommandService = new ProductCommandService(_productRepository.Object, _logger.Object);

            // Act & Assert
            Assert.Throws <Exception>(() => productCommandService.SaveProduct(product));
        }
コード例 #2
0
        public void SaveProduct_PositiveScenario()
        {
            // Arrange
            var product = new Product
            {
                Id          = 1,
                Code        = "Code",
                Description = "Description",
                Price       = 10
            };

            _productRepository.Setup(x => x.AddProduct(product)).Returns(product.Id);
            var productCommandService = new ProductCommandService(_productRepository.Object, _logger.Object);

            // Act
            int productId = productCommandService.SaveProduct(product);

            // Assert
            _productRepository.Verify(x => x.AddProduct(product), Times.Once);
            Assert.Equal(productId, product.Id);
        }