Wednesday, November 17, 2010

Unit testing with mock framework and IEquatable<>

This post is relevant for the one's who write tests, all you other programmers, you should learn unit testing and read it.

One of the most useful tools in unit testing is a good mocking framework. (I'm currently using Moq, and before that I used rhino)
We all know that for mocking we need interfaces.

My tip is very simple, if you have a class Foo and you wanna mock it, first you should have Interface IFoo (this should be obvious to most of you).
Implement on Foo IEquatable<IFoo>, so if you test a list of IFoo and you want to assert Contains()  you won't have any problems with the proxy of IFoo that mocking framework generates on the interface and no one will blame you for your awful class that throws cast exception in its equals method :).

Hope it'll save you time in future.

That it, KIS.