示例#1
0
        public async Task GivenValidCommand_Pass()
        {
            var id = await _handler.Handle(_command, CancellationToken.None);

            var product = await Context.Products.FirstOrDefaultAsync(p => p.Id == id);

            Assert.NotNull(product);
            Assert.Equal("test", product.Name);
            Assert.Equal(TestUserId, product.CreatedBy);
            Assert.Equal(DateTimeOffset.Now.Year, product.Created.Year);
        }
        public async Task CreateProductCommandHandlerAsync_WithNullParameter_ThrowsException()
        {
            //Arrange
            CreateProductCommandRequest command = null;

            //Act
            var actualException = await Assert.ThrowsAsync <Exception>(() => _createProductCommandHandler.Handle(command, new CancellationToken()));

            //Assert
            Assert.Equal(MessageConstants.CreateProductCommandRequestNull, actualException.Message);
        }
示例#3
0
        public async Task UpdateAsync()
        {
            var dataAccess = new ProductDataAccess(this.Context, Mapper());

            //Act
            var sutCreate    = new CreateProductCommandHandler(dataAccess);
            var resultCreate = await sutCreate.Handle(new CreateProductCommand
            {
                Data = ProductTestData.ProductDTO
            }, CancellationToken.None);

            //Act
            var sutUpdate    = new UpdateProductCommandHandler(dataAccess);
            var resultUpdate = await sutUpdate.Handle(new UpdateProductCommand
            {
                Data = new Common.DTO.ProductDTO
                {
                    Id   = resultCreate.Data.Id,
                    Name = "New pizza"
                }
            }, CancellationToken.None);

            //Assert
            Assert.IsTrue(resultUpdate.Succeeded);
        }
        public async Task <IActionResult> Create([Bind("Cost,Name")] Product product)
        {
            if (ModelState.IsValid)
            {
                await _handleCreateProductCommand.Handle(product);

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
        public void Should_ThrowArguementException_When_PriceIsNull()
        {
            var handler  = new CreateProductCommandHandler();
            var testName = Guid.NewGuid().ToString();
            var command  = new Commands.CreateProductCommand
            {
                Active = true,
                Name   = testName,
                Price  = null
            };

            Assert.ThrowsAsync <ArgumentException>(async() => await handler.Handle(command, default));
        }
示例#6
0
        public async Task SaveAsync()
        {
            var dataAccess = new ProductDataAccess(this.Context, Mapper());

            //Act
            var sutCreate    = new CreateProductCommandHandler(dataAccess);
            var resultCreate = await sutCreate.Handle(new CreateProductCommand
            {
                Data = ProductTestData.ProductDTO
            }, CancellationToken.None);

            Assert.IsTrue(resultCreate.Succeeded);
        }
示例#7
0
        public async Task PassCommand_ToCreateProductCommandHandler_ReturnsProductId()
        {
            // Arrange
            var repository = Substitute.For <IProductRepository>();

            repository.Create(Arg.Any <Product>()).Returns(1);
            var handler = new CreateProductCommandHandler(repository, Substitute.For <IMapper>());

            // Act
            var response = await handler.Handle(new CreateProductCommand(), new CancellationToken());

            // Assert
            Assert.Equal(1, response);
        }
        public async Task Should_ReturnNewId_When_ValidCreateProductCommandGiven()
        {
            var handler  = new CreateProductCommandHandler();
            var testName = Guid.NewGuid().ToString();

            var result = await handler.Handle(new Commands.CreateProductCommand
            {
                Active = true,
                Name   = testName,
                Price  = 100
            }, default);

            Assert.IsNotEmpty(result);
        }
        public async void CreateProductCommand_CreatesProduct()
        {
            // Arrange
            var mockProduct = _fixture.Build <CreateProductCommand>().Create();

            // Act
            var createProductCommand = new CreateProductCommandHandler(_petShopContext);
            var newProductId         = await createProductCommand.Handle(mockProduct, CancellationToken.None);

            // Assert
            var numberOfProducts = _petShopContext.Products.Count();

            Assert.Equal(1, numberOfProducts);
            Assert.NotEqual(Guid.Empty, newProductId);
        }
示例#10
0
        public async Task Should_Persist_Created_Product()
        {
            // Given
            var expectedCategoryId  = Random <int>();
            var expectedBrandId     = Random <int>();
            var expectedProductCode = Random <string>();

            // When
            await _sut.Handle(new CreateProductCommand(expectedCategoryId, expectedBrandId, expectedProductCode),
                              CancellationToken.None);

            // Then
            Verify(() => _repository.Save(Matches <Product>(c => c.Category.Id == expectedCategoryId &&
                                                            c.Brand.Id == expectedBrandId &&
                                                            c.Code == expectedProductCode),
                                          Ignore <CancellationToken>()));
        }
示例#11
0
        public async Task GetAllAsync()
        {
            var dataAccess = new ProductDataAccess(this.Context, Mapper());

            //Act
            var sutCreate    = new CreateProductCommandHandler(dataAccess);
            var resultCreate = await sutCreate.Handle(new CreateProductCommand
            {
                Data = ProductTestData.ProductDTO
            }, CancellationToken.None);

            //Act
            var sutGetAll    = new GetProductsQueryHandler(dataAccess);
            var resultGetAll = await sutGetAll.Handle(new GetProductsQuery(), CancellationToken.None);

            Assert.IsTrue(resultGetAll?.Data.Count == 1);
        }
示例#12
0
        public async void CreateProductCommand_NegativePrice_DoesNotCreateProduct()
        {
            // Arrange
            var mockProduct = _fixture.Build <CreateProductCommand>()
                              .With(p => p.Price, -10.00M)
                              .Create();

            // Act
            var mockHandler  = new CreateProductCommandHandler(_petShopContext);
            var newProductId = await mockHandler.Handle(mockProduct, CancellationToken.None);

            // Assert
            var products = await new ListAllProductsQueryHandler(_petShopContext)
                           .Handle(new ListAllProductsQuery(), CancellationToken.None);

            Assert.Empty(products);
            Assert.Equal(Guid.Empty, newProductId);
        }
        public async Task Handle_GivenValidCommand_InsertsNewProduct()
        {
            var command = new CreateProductCommand
            {
                CategoryId  = 1,
                SupplierId  = 1,
                ProductName = "Coffee",
                UnitPrice   = 3.7m
            };

            var sut = new CreateProductCommandHandler(_context);

            var productId = await sut.Handle(command, CancellationToken.None);

            productId.ShouldNotBe(0);

            var product = _context.Products.Find(productId);

            product.ShouldNotBeNull();
        }
示例#14
0
        public async Task GetAsync()
        {
            var dataAccess = new ProductDataAccess(this.Context, Mapper());

            //Act
            var sutCreate    = new CreateProductCommandHandler(dataAccess);
            var resultCreate = await sutCreate.Handle(new CreateProductCommand
            {
                Data = ProductTestData.ProductDTO
            }, CancellationToken.None);

            //Act
            var sutGet    = new GetProductQueryHandler(dataAccess);
            var resultGet = await sutGet.Handle(new GetProductQuery
            {
                Id = resultCreate.Data.Id
            }, CancellationToken.None);

            Assert.IsTrue(resultGet?.Data != null);
        }
示例#15
0
        public async Task DeleteAsync()
        {
            var dataAccess = new ProductDataAccess(this.Context);
            //Act
            var sutCreate    = new CreateProductCommandHandler(dataAccess);
            var resultCreate = await sutCreate.Handle(new CreateProductCommand
            {
                Data = ProductTestData.ProductDataDTO
            }, CancellationToken.None);


            //Act
            var sutDelete     = new DeleteProductCommandHandler(dataAccess);
            var outcomeDelete = await sutDelete.Handle(new DeleteProductCommand
            {
                Id = resultCreate.Data.Id
            }, CancellationToken.None);

            //Assert
            Assert.IsTrue(outcomeDelete.Succeeded);
        }
示例#16
0
        public async void ListProductsQuery_ReturnsAllProducts()
        {
            const int numberOfProductsToCreate = 5;

            // Arrange
            var createProducts =
                _fixture.Build <CreateProductCommand>().CreateMany(numberOfProductsToCreate);

            var createProductCommandHandler = new CreateProductCommandHandler(_petShopContext);

            foreach (var product in createProducts)
            {
                await createProductCommandHandler.Handle(product, CancellationToken.None);
            }

            // Act
            var mockHandler = new ListAllProductsQueryHandler(_petShopContext);

            var mockHandlerResult =
                await mockHandler.Handle(new ListAllProductsQuery(), CancellationToken.None);

            // Assert
            Assert.Equal(numberOfProductsToCreate, mockHandlerResult.Count());
        }