Archive for January, 2009


Seems that Pex is shipping with a new framework for creating “stubs” for use when creating unit tests.  When I first read about this, the source generation aspect made me skeptical.  However, the general design of these stubs I find quite intriguing. Traditional Mock frameworks (you need to understand the differences between various “test fakes”, but I’m not differentiating in this post) use code generation (Reflection.Emit) to create dynamic proxies.  Unfortunately, this means that when you have to stub out what you expect a method to return, for instance, you have to write a significant amount of code that, while usually “fluent”, isn’t very natural.  In fact, this sort of code in unit tests has been one of my least liked aspects of doing TDD.  The tests just aren’t easy to write, or to read.  However, the delegate based approach I find to be much easier to understand.  Maybe that’s just me, but I find it so much easier to code and later read and understand, that I’m switching immediately.

However, the Stubs framework itself isn’t something I’m going to make the switch to, at least today. The issue isn’t the source generation.  I’ve decided that’s not such an issue. The issue I have is simply one of licenses.  Nothing wrong with the Pex license, other than it’s not compatible with my needs.  Short term, I’m just going to start hand coding these stubs/fakes.  It’s not that difficult, though making the code pass StyleCop makes them a little more verbose than I’d like for such boilerplate stuff.  Long term, it might be worth looking into creating my own code generator for this. At some point, we can hope that the Pex Stubs library will move out of “research” and thus have a more widely usable license, but until then…


Read Full Post »