Testing Aggregates and Sagas with Message Flows
Testing Aggregate, Saga in PHP
On top of Messaging Test Support, Ecotone provides abstraction to handle Message Flows. This is a clean way to define step by step what happens in your system and verify the results.
Testing with Message Flows, make your code closer to production like execution. Message Flows are great candidate for writing acceptance tests or testing flow of your Aggregates and Sagas.
Setting up Flow Test
To enable Flow Test Support, as first parameter we pass list of classes we want to resolve for our testing scenario, this can be aggregate
, saga
or any class containing Ecotone's attributes
.
For more details on how to set up EcotoneLite for the different test scenario, read introduction chapter Testing Messaging.
Testing Aggregate or Sagas
It does not matter, if we test Saga or Aggregate, as the case will looks similar. So as an example, let's take Aggregate that looks like this:
When we've our Ecotone Lite set up, we may start using flow testing:
We set up our expected event as outcome of running
RegisterUser Command.
Then we can make use of Flow Support to send an Command and get event that was recorded as outcome.
You may send multiple command and chain them together, to build flow that you would like to test.
Verifying Aggregate's State
In cases when you're not using Event Sourced Aggregate, you may want to test state of the aggregate. We work only with high level API which are commands and we don't have direct access to Aggregate, Ecotone
however exposes a possibility to fetch aggregate inside the flow.
This
Command Handler
was registered by routing key without Command. With Flow support we may call Buses using routing key, and to tell which aggregate we want to call, we may useaggregate.id
metadata.Then we fetch given aggregate by id
And after that we can call method on aggregate that we've fetched
If you mark your query method with QueryHandler
attribute it becomes part of the flow, and we don't need to add ->addAggregateUnderTest(User::class)
and we may write following test instead:
Testing Communication between Aggregates and Sagas.
With Ecotone Lite
you may create full acceptance tests, just add classes that should be used in your test scenarios and let Ecotone glue everything together.
For given scenario you may add set of classes that should be used in this test.
You may include Interceptors
, Converters
, too.
In general all your Ecotone's production code will work in your test scenarios.
This ensures tests will be as close to production as it's possible.
Testing Output Channels
When we want to test given Aggregate or Sagas, yet we want to do it in isolation and we do use of Ecotone's Input/Output Pipes support. Then we may want to verify if given Message was send to given channel. Consider below example:
When Saga was started we will send an instance of TakePayment to "takePayment" output channel. However if our test suite does not include given Message Handler and we still want to hook to get if given Message was sent, we can do it as below:
Even if given Message is an Command from the perspective of outputChannel is just a simple Message. That's it won't be recorded as Command and we need to use getRecordedMessagePayloadsFrom
instead.
Testing with Real State-Stored Repository
By default Ecotone Lite provides for testing purposes In Memory Repositories. If you want to run your test against real database, you may disable this behaviour and provide your own repository.
You may also disable Event Sourced Repository:
Last updated