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 void CanCreate_CreateProductCommandHandler() { // Arrange var repository = Substitute.For <IProductRepository>(); // Act var handler = new CreateProductCommandHandler(repository, Substitute.For <IMapper>()); //Assert Assert.NotNull(handler); }
public ProductsController( CreateProductCommandHandler createProductCommandHandler, DeleteProductCommandHandler deleteProductCommandHandler, GetAllProductQueryHandler getAllProductQueryHandler, GetByIdProductQueryHandler getByIdProductQueryHandler) { _createProductCommandHandler = createProductCommandHandler; _deleteProductCommandHandler = deleteProductCommandHandler; _getAllProductQueryHandler = getAllProductQueryHandler; _getByIdProductQueryHandler = getByIdProductQueryHandler; }
public CreateProductCommandHandlerTests() { _mediatoR = new Mock <IMediator>(); var mockedProductRepository = new Mock <IProductRepository>(); var mockedCategoryRepository = new Mock <ICategoryRepository>(); _mockUnitOfWork = new Mock <IUnitOfWork>(); _mockUnitOfWork.Setup(x => x.ProductRepository).Returns(mockedProductRepository.Object); _mockUnitOfWork.Setup(x => x.CategoryRepository).Returns(mockedCategoryRepository.Object); _createProductCommandHandler = new CreateProductCommandHandler(_mockUnitOfWork.Object, _mediatoR.Object); }
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)); }
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); }
public ProductController(ILogger <ProductController> logger, CreateProductCommandHandler createProductCommandHandler, DeleteProductCommandHandler deleteProductCommandHandler, GetAllProductQueryHandler getAllProductQueryHandler, GetByIdProductQueryHandler getByIdProductQueryHandler ) { _logger = logger; _createProductCommandHandler = createProductCommandHandler; _deleteProductCommandHandler = deleteProductCommandHandler; _getAllProductQueryHandler = getAllProductQueryHandler; _getByIdProductQueryHandler = getByIdProductQueryHandler; }
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); }
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); }
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 CreateProductCommandHandlerTests() { _handler = new CreateProductCommandHandler(Context, Mapper); _command = new CreateProductCommand { Name = "test", ProductType = ProductType.Vip, Materials = new List <MaterialVm> { new MaterialVm { Name = "wood", Durability = TimeSpan.FromDays(2 * 365) }, new MaterialVm { Name = "iron", Durability = TimeSpan.FromDays(4 * 365) } } }; }
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); }
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(); }
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); }
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()); }
public ProductsController(CoreDbContext context) { _context = context; _handleCreateProductCommand = new CreateProductCommandHandler(_context); }
public CreateProductCommandHandlerTests() { _repository = FakeAndRegister <IProductRepository>(); _sut = new CreateProductCommandHandler(_repository); }