solidservices
solidservices copied to clipboard
How to write unit tests with CQRS?
Hi Steven,
I'm struggling to find a good unit testing example when using CQRS and just wondering if you've any plans on adding some unit tests to the solution?
Failing that, what parts of the system would you usually expect to see tests for when using CQRS? If you could recommend any good resources on the web in this area, that would be good too.
Thanks.
I have no intentions to add any tests to this reference project.
what parts of the system would you usually expect to see tests for when using CQRS
I don't think the answer would be any different from any other system you write. You typically want to test everything. So you'll have both a set of unit and integration tests.
A good book about testing is Roy Osherove's The Art of Unit Testing Second edition. This book is an easy read, so this is something I can highly recommend. I haven't any good web resources at hand.
@GFoley83 test your query and command handlers using a Mocking library such as Moq.
Psuedocode example, won't compile....
private Mock<IRepository> _db;
private CreateContactHandler ICommandHandler<CreateContactCommand> subjectUnderTest;
setup()
{
_db = new Mock<IRepository>();
subjectUnderTest = new CreateContactHandler(_db);
}
[Test]
public void ItAddsContactToStorage()
{
var command = new CreateContactCommand();
subjectUnderTest.Handle(command);
Assert.That(_db.Insert).was.Called.With(command);
}
Bottom line - ensure you test all the code paths within your handlers.
@GuyHarwood thanks a lot, much appreciated.