testing Sagas. It is also possible to test the state of a saga?


We are starting to write tests for our saga.
Is seems I can test the commands but not the current state of a filed of the saga.
we have a couple of events which only store data for later. I also want to verify that they are set.

Is this possible? Am I missing something?

Best, Michael

There shouldn't be a reason to test the internal state of a saga (or
anything else for that matter) as this would lead to a coupling of
test code and production code. Instead the idea is to just use the
Saga test fixture to setup your Saga (given...) and finally publish
the event (whenPublishing...) which should trigger an observable
behaviour (expect...).

From experience: we ran into issues making events part of the sagas

state: events may evolve over time and might require an upcaster. But
so does you saga if it contains an old event structure as part of it's
serialized form. I'd be sure to make up my mind about this issue.

The approach I’d take here would be to look at why you need to store data for later. Presumably just storing data in the saga but never doing anything with it isn’t the end goal; instead, it’s needed because it will affect the saga’s responses to subsequent events. So the tests should look at the responses to those subsequent events. How the saga stores the data for later should be an implementation detail of no interest to other entities in the system.

In terms of Axon’s given/when/expect test structure, you’d pass the “save for later” events in the “given” part of the call to the test fixture, pass the “do something with the saved data” event to the “when”, and then in the “expect”, check that the commands or events published by the saga make use of the relevant bits of saved data.

With that setup, you can refactor the saga code (say, to only save specific fields rather than entire event objects) and be confident the change has no effect on the saga’s behavior.