I'm starting to write some unit test for my javascript code using qunit and mockjax. I'm following the repository principle for retrieving my data. In each repository are some ajax calls to urls that return me some json data.
In my unit test I want to fake those calls using mockjax. Everything works fine when I am specifying a proxy to a file with test json data but when I specify inline json date on the response property of mockjax, I get an error (readystate 0).
See my test:
asyncTest('getAll03', function () {
var id = $.mockjax({
url: 'myurl',
contentType: 'text/json',
response: function (settings) {
this.responseText = { test: "123" }; // my test data
start(); // needed because otherwise the test will keep running on and on
}
});
// The actual call
personRepository.getAll({
ready: function (persons) {
console.log(persons);
equal(2, 5, "..."); // temp equal to see if test is working.
start(); // Start validation
}
});
});
If I log the response of the mockjax call in my production code, I see it ends up in the ajax error handling method. I does contain my test data but with statusText "error" and readyState '0'.
Any help?