Mock objects are another one of those software concepts that are interesting but useless until you see how they can make life easier. A few of the talks at Code Camp helped me turn that corner and see that they’re quite useful for unit testing. Namely, they allow you to unit test a class while mocking its dependencies, which frees you to focus on the class being tested instead of dealing with the overhead of a real database or web service.
For an initial hands-on experience, I refactored some unit tests I wrote last week for a business class that called a web service. All that really needed to be tested was the handling of various data and exceptions from the web service, which were one-liners once I figured out the syntax of Moq. Writing tests this way also exposed some areas where code was overly coupled; my business class was doing a lot to manage the external service.
The tests are now quicker, the code feels a bit more solid, and I’ve got at least a foothold in understanding mocks for testing and their impact on software architecture.