public void Test_CommitOrderChanges()
        {
            var(orderItems, shippingInfo) = OrderTestHelper.Given_OrderDetailsIsReady();

            var order = OrderTestHelper.Given_OrderIsReady(orderItems, shippingInfo);

            order.Commit();

            order.UncommittedEvents.Should().BeEmpty();
            order.Subtotal.Should().Be(orderItems.Sum(p => p.UnitPrice));
        }
        public void Test_CreateOrder_and_SaveToRepository()
        {
            var(orderItems, shippingInfo) = OrderTestHelper.Given_OrderDetailsIsReady();

            var orderRepository = Substitute.For <IRepository <Order> >();
            var service         = new OrderApplicationService(orderRepository);

            service.CreateOrder(orderItems, shippingInfo);

            orderRepository.Received(1).Save(Arg.Any <Order>());
        }
        public void Test_Dispatch_Events_after_Events_were_Persisted()
        {
            var(orderItems, shippingInfo) = OrderTestHelper.Given_OrderDetailsIsReady();
            var order = Order.NewOrder(Guid.NewGuid());

            order.Create(orderItems, shippingInfo);

            var dispatcher = Substitute.For <IEventDispatcher>();
            var repository = new OrderRepository(dispatcher);

            repository.Save(order);
            dispatcher.Received(1).Dispatch(Arg.Any <IEnumerable <IDomainEvent> >());
        }
        public void Test_CreatOrder()
        {
            var(orderItems, shippingInfo) = OrderTestHelper.Given_OrderDetailsIsReady();

            var order = Order.NewOrder(Guid.NewGuid());

            order.Create(orderItems, shippingInfo);

            order.UncommittedEvents.Count.Should().Be(3);
            order.UncommittedEvents.FirstOrDefault()?.AggregateRootId.Should().Be(order.Id);
            order.OrderItems.Should().BeEquivalentTo(orderItems);
            order.ShippingInfo.Should().BeEquivalentTo(shippingInfo);
            order.Subtotal.Should().Be(orderItems.Sum(p => p.UnitPrice));
        }
        public void Test_Order_Aggregate_can_be_Save_and_Load()
        {
            var(orderItems, shippingInfo) = OrderTestHelper.Given_OrderDetailsIsReady();
            var order = Order.NewOrder(Guid.NewGuid());

            order.Create(orderItems, shippingInfo);

            var dispatcher = Substitute.For <IEventDispatcher>();
            var repository = new OrderRepository(dispatcher);

            repository.Save(order);

            var actual = repository.Load(order.Id);

            actual.Should().BeEquivalentTo(order);
        }