Ejemplo n.º 1
0
        public async Task GetCategoryById_Success_ReturnCategoryDto()
        {
            //Arrange
            var category = new Category()
            {
                CategoryId = Constants.CategoryId,
                Name       = "Phone",
                Thumbnail  = "no-image.jpg"
            };

            var products = new List <Product>()
            {
                new Product()
                {
                    ProductId   = Constants.ProductId,
                    BrandName   = "Pineapple",
                    ProductName = "PinePhone X",
                    CategoryId  = category.CategoryId,
                    Price       = 1200,
                    Stock       = 12,
                    Sku         = "12312",
                    Category    = category,
                    Images      = "no-images"
                }
            };

            await _fuhoDbContext.Categories.AddRangeAsync(category);

            await _fuhoDbContext.Products.AddRangeAsync(products);

            await _fuhoDbContext.SaveChangesAsync();

            var getCategoryByIdQuery = new GetCategoryByIdQuery()
            {
                CategoryId = Constants.CategoryId
            };

            //Act
            var sut    = new GetCategoryByIdHandler(_fuhoDbContext, _logger.Object);
            var result = await sut.Handle(getCategoryByIdQuery, CancellationToken.None);

            //Assert
            Assert.NotNull(sut);
            Assert.IsType <CategoryDto>(result);
        }
Ejemplo n.º 2
0
        public void WhenExistsIdShouldInvokeGetCategoryByIdOnlyOnceTime()
        {
            //Arrange
            var categoryId = 20;
            var command    = new GetCategoryById(categoryId);

            var mock = new Mock <IToDoTaskRepository>();

            var repository = mock.Object;

            var handler = new GetCategoryByIdHandler(repository);

            //Act
            handler.Execute(command);

            //Assert
            mock.Verify(r => r.GetCategoryById(categoryId), Times.Once());
        }
        public IActionResult Post(Models.InsertToDoTask model)
        {
            var categoryCommand = new GetCategoryById(model.CategoryId);
            var category        = new GetCategoryByIdHandler(_repository).Execute(categoryCommand);

            if (category == null)
            {
                return(NotFound("Categoria não encontrada!"));
            }

            var command = new InsertToDoTask(model.Title, category, model.Deadline);
            var handler = new InsertToDoTaskHandler(_repository, _logger);
            var result  = handler.Execute(command);

            if (result.IsSuccess)
            {
                return(Ok());
            }

            return(StatusCode(500));
        }