I have a reasonably complex program architecture with minimal logic but lots of event handler passing. I’m making a skeleton of a server framework, and I have the following classes:
Core, which listens to:
CommHandler, which listens to:
CommLayer, which listens to:
IServerMock, which I stub out with a mocking framework (MOQ).
What’s the best way to unit test relationships like this that are triggered by events? I know unit tests are supposed to be very isolated and granular things, but the only way I can think of testing this is by testing the entire process and checking the final output from Core.
You need to make an abstraction for each class so
Coredepends on anICommHandlernotCommHandlerand so-on. The dependencies should be supplied via the constructor and that way you can easily test each class in isolation as you can mock it’s dependancies.In your production code you can then do this:
Although it would be better to use an IOC container to manage the instantiation of the objects since
CommHandlerhas it’s own dependencies but you should look into that separately.Then in your Unit Test, you can do this:
Doing this allows you to replace the implementation of ICommHandler in your test with a mock which you can configure the behaviour of.