But here at Hoopla, Trotter and I have found a very happy middle ground. As outlined in his post, it’s trivial to make a simple mock Raphael that satisfies your code and allows you to test what got called. But in practice, maintaining these mock classes gets repetitive quickly.
So I wrote recorderMock.js, which makes it easy to create full-featured mock objects for things like jQuery, Raphael and any other library that doesn’t run outside of the browser.
Say you wanted to test some code that used jQuery to resize a div:
I think I just heard your inner tester say “oh shit.” Worry no longer my friend. Building a mock for this with recorderMock is super simple. Just put this in your setup code.
You can even check that they got called with the right values:
And specifying return values is only slightly more complicated. Say you want to return mock html for different DOM elements:
Mock it out like this:
Need a basic mock of Raphael? Just use this:
So there you have it. You can indeed test browser-related code outside a browser. And it’s pretty easy.
I just released version 0.3.0 which includes better support for inspecting the call chain. The above examples have been updated to reflect the API changes and enhancements.