public async Task Get_invoice_handler_should_be_called_the_same_number_of_times_as_get_invoice_query(int count)
        {
            // Arrange
            ServiceCollection services = TestHelper.PrepareServiceCollection();

            services.AddSingleton <Counter>();

            // Replace the registered event class
            ServiceDescriptor serviceDescriptor = services.FirstOrDefault(d => d.ServiceType == typeof(IRequestHandler <GetInvoiceQuery, GetInvoiceQueryResponse>));

            services.Remove(serviceDescriptor);
            services.AddScoped <IRequestHandler <GetInvoiceQuery, GetInvoiceQueryResponse>, FakeGetInvoiceHandler>();

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();

                Counter counter = scopedServices.GetRequiredService <Counter>();

                // Act
                for (int i = 0; i < count; i++)
                {
                    GetInvoiceQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoiceQuery(Guid.NewGuid()));
                }

                // Assert
                counter.Get().Should().Be(count);
            }
        }
        public async Task <IActionResult> GetInvoice([FromRoute] GetInvoiceRequest query)
        {
            GetInvoiceQueryResponse result = await _messageManager.SendCommand(new GetInvoiceQuery(query.Id));

            if (result.Invoice is null)
            {
                return(NotFound("Not found"));
            }

            var response = new InvoiceResponse(
                new InvoiceDto(result.Invoice.Id, result.Invoice.Number, result.Invoice.CreationDate),
                StatusCodes.Status200OK);

            return(Ok(response));
        }
        public async Task All_created_invoices_should_be_able_to_get_by_passing_their_id(int count)
        {
            ServiceProvider serviceProvider = TestHelper.PrepareServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();

                // Arrange

                // empty repository
                var invoiceRepository = scopedServices.GetRequiredService <IInvoiceRepository>();

                List <CreateInvoiceCommandResponse> createInvoiceResponses = new List <CreateInvoiceCommandResponse>();

                // Act
                for (int i = 0; i < count; i++)
                {
                    CreateInvoiceCommandResponse createInvoiceResponse = await messageManager.SendCommand(
                        new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: "JK/02/2019", creationDate: DateTime.Now)));

                    createInvoiceResponses.Add(createInvoiceResponse);
                }

                // Assert
                foreach (var createdInvoice in createInvoiceResponses)
                {
                    GetInvoiceQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoiceQuery(createdInvoice.Id));

                    queryResponse.Invoice.Should().NotBeNull();
                    queryResponse.Invoice.Id.ToString().Should().BeEquivalentTo(createdInvoice.Id.ToString());
                }
            }
        }