Snowing Code

Personal notes on software development

Jasmine Testing Jquery Templates in ASP.NET MVC

(Publish date: 04/10/2011)

a few weeks ago Derick Bailey posted about using jasmine-jquery fixtures to test your view templates rendering methods. While his post focused on the ruby echosystem, using sinatra, he did mention this should be fairly straight forward to achieve in other web environments, such as mvc. Since I was and still am working on a backbone example project in mvc, and am using jquery templates in some of my views render methods, I wanted to give this a go.

At first I thought I will have to specify as my fixture base path my localhost templates url, which made me think this might end up in a cross-origin ajax request made by the Jasmine jquery add on. Thankfully that was far from the truth; it turns out the rack application that the Jasmine gem is based on is far more flexible than what I feared it would be at first, and it would quite simply render any of the files within my application directory, which is shared by the Jasmine gem local server. With all that in mind, let's have a look first at the simple solution I came up for rendering the templates:

And calling the template from the view would be done like so:

The partial view containing the template will then look like so:

You may see some odd behaviour and get some errors on certain browsers, if you leave the acsx header on: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> The javasript we need to add in order for all of this to work would then look like that:

If you'd like to see how all of this application looks like in its entirety, head over to my backbone test application repository on github.

blog comments powered by Disqus