How to unit test javascript function that calls getJSON

爱⌒轻易说出口 提交于 2019-12-11 03:39:08

问题


I've been struggling with unit test for 2 days now and there is something I can't achieve regarding async test. I'm new to unit test and I don't understand why this doesn't work.

I have a file login.js that calls a $.getJSON(url, data, function) and returns a string with the status of login ("success" or "fail"). The call to $.getJSON uses mockjax to get the data (it wraps an ajax call).

The login function works ok, when called from a test webpage using jQuery click event. But now I'm trying to run headless test using Qunit and PhantomJS.

It seems the problem is that the test is not waiting for the $.getJSON call to get the result (even when using a timeout). Any clues? Here is the code.

login.js

var login = function(user, pass){
    $.getJSON("loginURL", {"user":user, "pass":pass}, function(response){
       if(response.status === "success"){
           //do something
           return "success";
       } 
       else{
           //do something else
           return "fail";
       } 
    });
};

test.js

test("Test login", function(){
    var user = "user1", pass = "pass1";
    var done = assert.async();
    var result = login(user, pass);
    setTimeout(function(){
    assert.equal(result, "success", "expect login succeded");
    done();
    },1000);
});

In the test result I get:

Expected: "success"

Result: undefined


回答1:


Your login function should be asynchronous, because its result depends on a response from server.

So let's rewrite the function like this:

function login(user, pass, done) {
    $.getJSON(..., function (res) {
        done(res.status == 'success')
    })
}

Then you can test like this (assuming mocha):

describe('...', function () {
    it('login()', function (done) {
        login('user', 'pw', function (isLoggedIn) {
            assert(isLoggedIn)
            done()
        })
    })
})



回答2:


After rewrite my login function as @ayanami suggested the code works smoothly and it looks like this:

login.js

var login = function(form, cb){
   $.getJSON("loginURL", {"user":user, "pass":pass}, function(response){
        if(response.status === "success"){
            //do something
        }
        else{
            //do something else
        }
       cb(response.status);
    });
};

test.js (qunit)

test( "Testing login", function( assert ) {
    var done = assert.async();
    var cb = function(param){
                assert.equal(param,"success", "Expect getJSON/login to success");
                done();
            };
    login(user, pass,cb);
});


来源:https://stackoverflow.com/questions/28826974/how-to-unit-test-javascript-function-that-calls-getjson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!