2015-05-23 185 views
0

我試圖創建一個函數測試XMLHttpRequest(帶回調參數)。但它總是返回undefinedQUnit XMLHttpRequest測試

function sendMessage(serverFile, data, retVal) { 
 
    var xhr = new XMLHttpRequest(); 
 

 
    xhr.open('POST', serverFile, true); 
 
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
 

 
    xhr.onreadystatechange = function() { 
 
    if (xhr.readyState == 4) { 
 
     switch (xhr.status) { 
 
     case 200: 
 
      retVal(xhr.responseText); 
 
      break; 
 
     case 404: 
 
      retVal("404"); 
 
      break; 
 
     case 0: 
 
      retVal("0"); 
 
      break; 
 
     default: 
 
      retVal("Other Code"); 
 
     } 
 
    } 
 
    } 
 

 
    xhr.send("data="+data); 
 
}

所有服務器上的文件都很好。

這是測試腳本。

QUnit.test("Message Send XMLHttpRequest Test", function(assert) { 
 
    assert.expect(5); 
 
    var done1 = assert.async(); 
 
    var done2 = assert.async(); 
 
    var done3 = assert.async(); 
 
    var done4 = assert.async(); 
 
    var done5 = assert.async(); 
 

 
    window.setTimeout(function() { 
 
    var ret; 
 
    sendMessage("server.php", "Hello Text Message", function(responseText) { 
 
     ret = responseText; 
 
    }); 
 
    assert.equal(ret, "Message Correct", "Sending normal message"); 
 
    done1(); 
 
    }, 0); 
 

 
    window.setTimeout(function() { 
 
    var ret; 
 
    sendMessage("server.php", "<script>Message with Tags<script>", function(responseText) { 
 
     ret = responseText; 
 
    }) 
 
    assert.equal(ret, "Message Error", "Sending message with tags"); 
 
    done2(); 
 
    }, 100); 
 

 
    window.setTimeout(function() { 
 
    var ret; 
 
    sendMessage("wrong.php", "Hello Text Message", function(responseText) { 
 
     ret = responseText; 
 
    }) 
 
    assert.equal(ret, "404", "Requesting wrong server file"); 
 
    done3(); 
 
    }, 100); 
 

 
    window.setTimeout(function() { 
 
    var ret; 
 
    sendMessage("http://wrongDomain.wrong/server.php", "Hello Text Message", function(responseText) { 
 
     ret = responseText; 
 
    }) 
 
    assert.equal(ret, "0", "Requesting wrong server(or server has no answer)"); 
 
    done4(); 
 
    }, 100); 
 

 
    window.setTimeout(function() { 
 
    var ret; 
 
    sendMessage("otherCode.php", "Hello Text Message", function(responseText) { 
 
     ret = responseText; 
 
    }) 
 
    assert.equal(ret, "Other Code", "Request returns other code"); 
 
    done5(); 
 
    }, 100); 
 
});

這裏RET永遠是不確定的。 我做錯了什麼?

回答

0

我找到了答案!

QUnit.test("Message Send XMLHttpRequest Test", function(assert) { 
 
    assert.expect(5); 
 

 
    var done = []; 
 
    for (var i = 0; i < 5; i++) { 
 
    done[i] = assert.async(); 
 
    }; 
 

 
    sendMessage("server.php", "Hello Text Message", function(responseText) { 
 
    assert.equal(responseText, "Message Correct", "Sending normal message"); 
 
    done[0](); 
 
    }); 
 
    sendMessage("server.php", "<script>Message with Tags<script>", function(responseText) { 
 
    assert.equal(responseText, "Message Error", "Sending message with tags"); 
 
    done[1](); 
 
    }); 
 
    sendMessage("wrong.php", "Hello Text Message", function(responseText) { 
 
    assert.equal(responseText, "404", "Requesting wrong server file"); 
 
    done[2](); 
 
    }); 
 
    sendMessage("http://wrongDomain.wrong/server.php", "Hello Text Message", function(responseText) { 
 
    assert.equal(responseText, "0", "Requesting wrong server(or server has no answer)"); 
 
    done[3](); 
 
    }); 
 
    sendMessage("otherCode.php", "Hello Text Message", function(responseText) { 
 
    assert.equal(responseText, "Other Code", "Request returns other code"); 
 
    done[4](); 
 
    }); 
 
});