public void PurchaseService_RemoveItemAsync_DeveRemoverOItemQuandoOMesmoExistir()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Act
            _purchaseService.RemoveItemAsync(purchase, purchaseItens.First());

            // Assert
            purchase.Items.Should().HaveCount(totalItens - 1);
            purchase.TotalValue.Should().Be(purchaseItens.Skip(1).Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Remove(It.IsAny <Purchase>()), Times.Once);
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens + 1));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseItemRemoved>()), Times.Once);
        }
        public void PurchaseService_AddItemAsync_DeveAdicionarOsItensQuandoEmEstadoValido()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            // Act
            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Assert
            purchase.Items.Should().HaveCount(totalItens);
            purchase.TotalValue.Should().Be(purchaseItens.Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Update(It.IsAny <Purchase>()), Times.Exactly(totalItens));
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseItemAdded>()), Times.Exactly(totalItens));
        }
        public void PurchaseService_CloseAsync_DeveMudarOEstadoParaFechadoCorretamenteQuandoEstiverAberto()
        {
            // Arange
            int totalItens = 5;

            Purchase             purchase      = PurchaseFaker.GenerateFaker().Generate();
            IList <PurchaseItem> purchaseItens = PurchaseItemFaker.GenerateFaker(purchase).Generate(totalItens);

            ConfigureMock();

            FactoryService();

            foreach (PurchaseItem purchaseItem in purchaseItens)
            {
                _purchaseService.AddItemAsync(purchase, purchaseItem);
            }

            // Act
            _purchaseService.CloseAsync(purchase);

            // Assert
            purchase.State.Should().Be(PurchaseState.Closed);
            purchase.Items.Should().HaveCount(totalItens);
            purchase.TotalValue.Should().Be(purchaseItens.Sum(x => x.CalculteValue()));
            _purchaseRepositoryMock.Verify(x => x.Update(It.IsAny <Purchase>()), Times.Exactly(totalItens + 1));
            _unitOfWork.Verify(x => x.CommitAsync(), Times.Exactly(totalItens + 1));
            _busHandlerMock.Verify(x => x.PublishDomainEvent(It.IsAny <PurchaseClosedEvent>()), Times.Once);
        }
示例#4
0
        public void Purchase_UpdateItem_DeveGerarDomainExceptionQuandoTentarAtualizarUmItemInexistente()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            PurchaseItem purchaseItem = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            // Act
            Action act = () => purchase.UpdateItem(purchaseItem);

            // Assert
            act.Should().Throw <DomainException>();
        }
示例#5
0
        public void Purchase_AddItem_DeveGerarDomainExceptionQuandoAdicionarUmItemEEstiverFechado()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            purchase.Close();

            PurchaseItem item1 = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            // Act
            Action act = () => purchase.AddItem(item1);

            // Assert
            act.Should().Throw <DomainException>();
        }
示例#6
0
        public void Purchase_CalculateTotalValue_DeveSomarCorretamenteValorTotal()
        {
            // Arange && Act
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(10);

            foreach (PurchaseItem itemMock in itemsMock)
            {
                purchase.AddItem(itemMock);
            }

            // Assert
            purchase.TotalValue.Should().Be(itemsMock.Sum(x => x.CalculteValue()));
        }
示例#7
0
        public void Purchase_RemoveItem_DeveGerarDomainExceptionQuandoRemoverUmItemInexistente()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(2);

            purchase.AddItem(itemsMock.First());

            // Act
            Action act = () => purchase.RemoveItem(itemsMock.Last());

            // Assert
            act.Should().Throw <DomainException>();
        }
示例#8
0
        public void Purchase_UpdateItem_DeveAtualizarCorretamenteUmItemQuandoOMesmoExistir()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            PurchaseItem purchaseItem = PurchaseItemFaker.GenerateFaker(purchase).Generate();

            purchase.AddItem(purchaseItem);

            // Act
            purchase.UpdateItem(purchaseItem);

            // Assert
            purchase.Items.Should().HaveCount(1);
        }
示例#9
0
        public void Purchase_AddItem_DeveAdicionarOsItensCorretamenteQuandoValidos()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            // Act
            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(10);

            foreach (PurchaseItem itemMock in itemsMock)
            {
                purchase.AddItem(itemMock);
            }

            // Assert
            purchase.Items.Should().HaveCount(10);
        }
示例#10
0
        public void Purchase_RemoveItem_DeveRemoverCorretamenteQuandoItemExistir()
        {
            // Arange
            Purchase purchase = PurchaseFaker.GenerateFaker().Generate();

            IList <PurchaseItem> itemsMock = PurchaseItemFaker.GenerateFaker(purchase).Generate(2);

            purchase.AddItem(itemsMock.First());
            purchase.AddItem(itemsMock.Last());

            // Act
            purchase.RemoveItem(itemsMock.First());

            // Assert
            purchase.Items.Should().HaveCount(1);
            purchase.Items.Should().NotContain(x => x.Id == itemsMock.First().Id);
        }