public async Task GivenValidCommand_Pass()
        {
            await _handler.Handle(_command, CancellationToken.None);

            var product = await Context.Products.FirstOrDefaultAsync(u => u.Id == _command.Id);

            Assert.Equal(TestUserId, product.LastModifiedBy);
            Assert.Equal(DateTimeOffset.Now.Year, product.LastModified?.Year);
        }
        public async Task UpdateProductCommandHandlerAsync_WithNullParameter_ThrowsException()
        {
            //Arrange
            UpdateProductCommandRequest command = null;

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

            //Assert
            Assert.Equal(MessageConstants.UpdateProductCommandRequestNull, actualException.Message);
        }
        public async Task UpdateProductCommandHandler_ThrowsChangesNotSavedCorrectlyException()
        {
            //Arrange
            productRepository.Setup(x => x.GetById(It.IsAny <int>())).Returns(Task.FromResult(product));
            productRepository.Setup(x => x.SaveAllAsync()).ThrowsAsync(new ChangesNotSavedCorrectlyException(typeof(Product)));

            //Act
            var action = await commandHandler.Handle(command, It.IsAny <CancellationToken>());

            //Assert
            Assert.Equal(Unit.Value, action);
            mapper.Verify(x => x.Map(command, product), Times.Once);
        }
Пример #4
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);
        }