Why is mockjax returning readystate 0 on inline json responsetext?

645 views Asked by At

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?

1

There are 1 answers

0
Bas Slagter On BEST ANSWER

Found the answer.

It turns out that defining the content type as a global setting for mockjax is the culprit. Removing this setting solved the problem. I don't know for sure but it seems to be a bug in the mockjax library. Like I said, this is not a problem when you define a proxy on your mockjax object. Only when you define an inline response it seems to only result in readystate 0.