I have a dynamic page built using jQuery. Html parts are loaded from mustache templates. These templates are loaded from the URL, and I would like to unit test the general html construct:
JsTestDriver Test:
AppTest = TestCase("AppTest") AppTest.prototype.test = function() { var actualHtml = ""; getHtml({ "title": "title", "header": "header", "text": "text", "authors": [ {"firstname": "firstname", "lastname": "lastname"} ] }, function(html) { actualHtml = html; }); assertEquals("expected html", actualHtml); };
And the code:
function getHtml(json, resultFunc) { jQuery.ajax({ url: "url/to/mustache/template", success: function(view) { resultFunc(mergeArticleModelAndView(json, view)); }, error: function(jqXHR, textStatus, errorThrown) { resultFunc(textStatus + " errorThrown: " + errorThrown); }, dataType: 'text', async: false }); }
Then I run the tests, and the result:
$ java -jar JsTestDriver-1.3.2.jar --port 9876 --browser /usr/bin/firefox --tests all F Total 1 tests (Passed: 0; Fails: 1; Errors: 0) (8,00 ms) Firefox 5.0 Linux: Run 1 tests (Passed: 0; Fails: 1; Errors 0) (8,00 ms) AppTest.test failed (8,00 ms): AssertError: expected "expected html" but was "error errorThrown: [Exception... \"Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)\" nsresult: \"0x80004005 (NS_ERROR_FAILURE)\" location: \"JS frame :: http://localhost:9876/test/main/js/jquery.min.js :: <TOP_LEVEL> :: line 16\" data: no]" ()@http://localhost:9876/test/test/js/app_test.js:25
So, the error callback was called, and I donβt understand why it breaks with JsTestDriver, and the code works when calling the application manually using the browser
Last thing, jsTestDriver.conf:
server: http://localhost:9876 load: - test/js/app_test.js - main/js/jquery.min.js - main/js/jquery.mustache.js - main/js/app.js
Thanks for your advice. More generally, what unit test frameworks do you use to test javascript and command line using DOM and jQuery?