gpt4 book ai didi

javascript - 如何使用 Jasmine 测试 XMLHttpRequest

转载 作者:可可西里 更新时间:2023-11-01 01:55:06 24 4
gpt4 key购买 nike

如何在没有 jQuery 的情况下测试 XMLHttpRequest 或纯 Javascript AJAX 上的 onreadystatechange?我这样做是因为我正在开发 Firefox 扩展。我想我必须使用 spy ,但不知道如何使用,因为我的 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。

关于javascript - 如何使用 Jasmine 测试 XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8901445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com