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.



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 Todd fixtures.

Download Sample Code


GAX/GAT February 2008 Release

Finalmente y luego de un gran esfuerzo, tenemos un release final de GAX/GAT February 2008 Release.

Principalmente los mejoras mas importantes son que ahora puedes utilizar GAX/GAT side by side con Visual Studio 2005 y Visual Studio 2008, tambien ahora no tienes que hacer la dolorosa tarea de desregistar o desinstalar todos los guidance packages existentes antes de desinstalar GAX para el posterior upgrade. De todas formas en este release en particular hemos hecho un Upgrade en lugar de un Installer con lo cual ni siquiera hace falta desinstalarlo, la nueva version se instalara arriba de la anterior haciendo un upgrade de todos los guidance packages existentes previamente.

Algunos de las mejoras para este release son:

  • Side by Side con Visual Studio 2005 y 2008
  • Upgrade en lugar de Installer, no es necesario deinstalar guidance packages antes de instalar nuevas versions.
  • Ahora es posible desinstalar gax sin desregistrar guidance packages, GAX hara este trabajo.
  • Algunos fixes de bugs en GAX para Visual Studio 2008 trabajando en el Experimental Hive.

Puedes chequear el blog de Grigori’s para obtener mas informacion acerca de este release.