thenativeweb / cqrs-sample

CQRS, EventSourcing, (DDDD) Sample in node.js
148 stars 50 forks source link

Best practice in writing tests? #20

Closed Jan-Jan closed 7 years ago

Jan-Jan commented 7 years ago

Hi Adrai,

Thanks a lot for this sample app, and the rest of the cqrs-code -- much appreciated.

I was just wondering if there was a testing best practice that you used? The key testing point for me would be how events are applied to the aggregate. However, the way the code is structured, the callback used to change the aggregate only gets exposed/exported as a require('cqrs-domain').defineEvent(options, callback), which seemingly makes it hard to get to.

adrai commented 7 years ago

Thx a lot, Every of these cqrs modules can be tested like black-box testing. Every persistence part has an in-memory implementation. So for example for cqrs-domain you can handlea command and assert the resulting events and even the aggregate data: like -> https://github.com/adrai/node-cqrs-domain/blob/master/test/integration/integrationTest.js#L826 We test everything like that.

Jan-Jan commented 7 years ago

Thank you! This helps me with testing command -> event.

However, what I want to test here is the delta in aggregate that an event causes, ie, I supply an aggregate, then the event causes a change, which I would like to test. (I cannot see an example of that in the test code.)

adrai commented 7 years ago

Bring your aggregate in you "starting position" by handling your commands. Catch the aggregateData of the last handled command. Then start with your test case and create a diff of the new aggregateData... btw. my advice is to test your intension (commands/events) not the aggregateData (it's just a temporal in-memory state that helps you enforce your business rules)