Testing domain logic
Specify the expected events to be published as a result of the given command. Simple!
describe 'MyNamespace.myBoundedContext.MyAggregate', ->
beforeEach ->
@app = new MyApp()
@app.start()
@aggregateId = new Guid()
@data = {
someValue: 'TestValue'
}
@anotherValue = 'TestValue2'
it 'generates the MyAggregate created event', ->
@app.given(
new MyNamespace.myBoundedContext.CreateMyAggregate _.extend {}, @data, {
targetId: @aggregateId
timestamp: new Date()
}
)
.expect([
new MyNamespace.MyAggregateCreated _.extend {}, @data, {
sourceId: @aggregateId
}
])
it 'generates the MyAggregate created event', ->
@app.given(
new MyNamespace.MyAggregateCreated _.extend {}, @data, {
sourceId: @aggregateId
timestamp: new Date()
}
)
.when([
new MyNamespace.myBoundedContext.DoSomething({
targetId: @aggregateId
timestamp: new Date()
someValue: @anotherValue
})
])
.expect([
new MyNamespace.MyAggregateChanged _.extend {}, @data, {
sourceId: @aggregateId
}
])
What about unit testing in the domain?
One advantage of an event-sourced system based on the CQRS pattern is we can effectively omit fine-grained and arguably brittle unit tests since we really only need to check the events generated as a result of a command being sent in on the Command Bus. The messaging and event-sourcing libraries provide test coverage for reliable delivery, so you can focus on writing tests that ensure the aggregates are delivering the business requirements and are protecting themselves from invariants.
