예제 #1
0
        public async Task Get_ProductsWithoutResult_Ok()
        {
            var productsDto = UtilityTest.GenerateProductDtoInDifferenttegoryAsync();

            var _mediatorMock = new Mock <IMediatorService>();

            _mediatorMock.Setup(x => x.Send(It.IsAny <GetAllProductQuery>(),
                                            It.IsAny <CancellationToken>()))
            .ReturnsAsync(productsDto);

            var _loggerMock = new Mock <ILogger <ProductsController> >();

            var _mockMapper = new Mock <IMapper>();



            var productsController = new ProductsController(_loggerMock.Object, _mediatorMock.Object, _mockMapper.Object);
            var actionResult       = await productsController.GetProducts();



            var viewResult = Assert.IsType <Microsoft.AspNetCore.Mvc.OkObjectResult>(actionResult);

            Assert.IsAssignableFrom <List <ProductDto> >(viewResult.Value);
        }
예제 #2
0
        public async Task Get_ProductSingleWithoutResult_NotFound()
        {
            var productDto = UtilityTest.GenerateProductDtoInDifferenttegoryAsync().First();

            var modelView = new ProductModelView
            {
                ProductId    = productDto.ProductId,
                CategoryId   = productDto.CategoryId,
                Name         = productDto.Name,
                CategoryName = "CategoryName",
                Description  = "Desc",
                Note         = "Note",
                Options      = new Dictionary <int, string> {
                    { 1, "Bianco" }
                }
            };

            var _mediatorMock = new Mock <IMediatorService>();

            _mediatorMock.Setup(x => x.Send(It.IsAny <GetProductByIdQuery>(),
                                            It.IsAny <CancellationToken>()))
            .ReturnsAsync((ProductModelView)null);

            var _loggerMock = new Mock <ILogger <ProductsController> >();

            var _mockMapper = new Mock <IMapper>();



            var productsController = new ProductsController(_loggerMock.Object, _mediatorMock.Object, _mockMapper.Object);
            var actionResult       = await productsController.GetProduct(100);



            var viewResult = Assert.IsType <Microsoft.AspNetCore.Mvc.NotFoundObjectResult>(actionResult);

            Assert.IsAssignableFrom <string>(viewResult.Value);
            Assert.Equal("Product id 100 not found", viewResult.Value);
        }