コード例 #1
0
        public async void GetShouldReturnProducts()
        {
            // Arrange
            var products = new List<Product>
            {
                new Product {ProductId = 1, ProductName = "Product1"},
                new Product {ProductId = 2, ProductName = "Product2"},
                new Product {ProductId = 3, ProductName = "Product3"},
            };

            // Mock products repo
            var productsRepoMock = new Mock<IProductsRepository>();
            productsRepoMock.Setup(m => m.GetProducts()).ReturnsAsync(products);

            // Mock unit of work
            var unitofWorkMock = new Mock<INorthwindUnitOfWork>();
            unitofWorkMock.SetupGet(m => m.ProductsRepository).Returns(productsRepoMock.Object);

            // Create controller
            var productsController = new ProductsController(unitofWorkMock.Object);

            // Act
            IHttpActionResult response = await productsController.Get();

            // Assert
            var actual = ((OkNegotiatedContentResult<IEnumerable<Product>>)response).Content;
            var comparer = new GenericComparer<Product>(
                (p1, p2) => p1.ProductId == p2.ProductId);
            Assert.Equal(products, actual, comparer);
        }
コード例 #2
0
        public async void GetShouldReturnProducts()
        {
            // Arrange
            var expected = new List<Product>
            {
                new Product {ProductId = 1, ProductName = "Product1"},
                new Product {ProductId = 2, ProductName = "Product2"},
                new Product {ProductId = 3, ProductName = "Product3"},
            };
            IProductsRepository productsRepo = new MockProductsRepository(expected);
            INorthwindUnitOfWork unitOfWork = new MockNorthwindUnitOfWork(productsRepo);
            var productsController = new ProductsController(unitOfWork);

            // Act
            IHttpActionResult response = await productsController.Get();

            // Assert
            var actual = ((OkNegotiatedContentResult<IEnumerable<Product>>)response).Content;
            var comparer = new GenericComparer<Product>(
                (p1, p2) => p1.ProductId == p2.ProductId);
            Assert.Equal(expected, actual, comparer);
        }