如何在没有 jQuery 的情况下测试 XMLHttpRequest 或纯 Javascript AJAX 上的 onreadystatechange ?我这样做是因为我正在开发 Firefox 扩展。我想我必须使用间谍,但不知道如何使用,因为我的ajax不会返回任何内容。
submit : function() {
var url = window.arguments[0];
var request = new XMLHttpRequest();
request.open("POST", 'http://'+this.host+'/doSomething', true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("param="+param+"&emotions;="+this.getParams());
request.onreadystatechange = function() {
if(this.readyState == 4) {
// alert(this.responseText);
}
};
}
那么这个呢?
beforeEach(function() {
// spyOn(XMLHttpRequest.prototype, 'open').andCallThrough(); // Jasmine 1.x
spyOn(XMLHttpRequest.prototype, 'open').and.callThrough(); // Jasmine 2.x
spyOn(XMLHttpRequest.prototype, 'send');
});
...
it("should call proper YQL! API", function() {
podcast.load_feed('http://www.faif.us/feeds/cast-ogg/');
expect(XMLHttpRequest.prototype.open).toHaveBeenCalled();
});
纯粹的 Jasmine,无需使用任何外部库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)