Jose Escrich

Extensibility Thoughts

February 2008 - Posts

Testing T4 templates using mock framework (MoQ)

Some time ago I wrote a post in which I talk about how you can do unit tests over your text templates using the GAX Host. In that example I used a very simple template which receives just a couple of string properties. But it becomes more complicated when you have properties which are complex types ok I know, most probably you're thinking that can resolve that just creating your own mock objects and you're right.

But the thing is that you can do that in a really easiest way using a mock framework. I will not speak here about mock frameworks so you can find a lot of posts about it. I'll speak about MoQ which is a new mock framework developed by Clarius and other companies. You can know more about MoQ in the Daniel's blog and in its google code page.

In the following example what I did was create a simple text template which uses an IEntitySchema object to create something like a Business Entity, so here we got an issue because we need an instance of that IEntitySchema and, the way to get it using the classic TDD approach, is creating an explicit Mock object.

So here is when MoQ comes to the rescue, if you see the code snippet it has a call to new Mock<IEntitySchema>() which will creates a proxy of an IEntitySchema implementation. After that, I defined our expectations for each interface signature in where we want to test an interaction.

image

image

So, at that point we're ready to perform the tests and make all the assertions, plus the really interesting thing, which is the verification of all our expectations. That basically means that, beside make the assertions over states, now we can check if some interactions was made or not (in other words if some method or some property in the interface has been called or not and if was called with certain parameters values and types or not).

I think that the test interactions are a really interesting thing to include in our classic tdd fixtures.

Download Sample Code

Enjoy.

Posted: Feb 27 2008, 05:48 PM by jescrich | with 1 comment(s)
Filed under: , , , ,
GAX/GAT February 2008 Release

Finally, and after a grand effort, we got a final release of GAX/GAT February 2008 Release.

Mainly the most important improvements are that now you can use GAX side by side with Visual Studio 2005 and Visual Studio 2008 and now you don't have to do the painful task to unregister all your guidance packages prior to uninstall the previous version of GAX. In this particular release, we made an Upgrade than an Installer so you won't have to uninstall GAX, it will be just a click on the msi to get it everything update without loss any guidance package previously registered.

Some of the improvements for this release are:

  • Support side by side for Visual Studio 2005 and Visual Studio 2008.
  • Upgrade than Installer, the current GAX installer is an upgrade, so you don't have to uninstall all your guidance packages then uninstall GAX before to try install it.
  • Now you can also uninstall GAX whenever you want without unregister any guidance packages, it will do that for you ;), anyway you also have a nice UI in the uninstaller which tell you what guidance packages you will lost if you continue the GAX uninstallation.
  • Some error bug fixes when you use GAX for Visual Studio 2008 and the Experimental hive.

You can check the Grigori's Blog to get more information about this release.

Downloads

GAX and GAT must be downloaded and installed separately. GAT requires that GAX is installed first.

Remember: If you have a previous version of GAX installed on Visual Studio 2005, it will be updated to the February 2008 release of GAX. You are no longer required to uninstall GAX and the corresponding guidance packages (with an exception of GAT, which must be re-installed).

Enjoy!.

More Posts