Javascript 将$.getJSON与jsonp一起使用时,Sinon.js fakeServer.request.respond失败
你好,我是sinon.js的新手。我正在写Jasmine BDD测试代码。我想制作一个小应用程序,从flickr获取照片Javascript 将$.getJSON与jsonp一起使用时,Sinon.js fakeServer.request.respond失败,javascript,sinon,Javascript,Sinon,你好,我是sinon.js的新手。我正在写Jasmine BDD测试代码。我想制作一个小应用程序,从flickr获取照片 describe("with stub", function() { beforeEach(function() { this.server = sinon.fakeServer.create(); this.server.autoRespond = true; this.server.respondWith(200, {
describe("with stub", function() {
beforeEach(function() {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.respondWith(200, {
"Content-Type": "application/json"
}, '{"photos":{"page":1, "pages":726, "perpage":5, "total":"3630", "photo":[{"id":"8591804280", "owner":"77921082@N00", "secret":"da96195b4b", "server":"8526", "farm":9, "title":"Pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591810388", "owner":"77921082@N00", "secret":"d94ce346a5", "server":"8509", "farm":9, "title":"Street Plate", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591801040", "owner":"77921082@N00", "secret":"cb7b1e246a", "server":"8097", "farm":9, "title":"Stone pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590414659", "owner":"77921082@N00", "secret":"fb49a25607", "server":"8094", "farm":9, "title":"Street pole", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590411479", "owner":"77921082@N00", "secret":"9aab17d3a9", "server":"8370", "farm":9, "title":"Street plate", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}');
this.flickrPhotos = this.flickr.photos;
});
afterEach(function() {
this.flickrPhotos = [];
});
it("[0].title should be Pod", function() {
this.flickr.getData(5, true);
expect(this.flickrPhotos[0].title).toBe("Pod");
});
});
以下代码未通过测试。错误TypeError:无法读取返回的未定义的属性“title”
root.Flickr = (function() {
function Flickr(number) {
this.number = number;
this.photos = [];
}
Flickr.prototype.getData = function(number) {
var _this = this;
$.getJSON('http://www.flickr.com/services/rest/?jsoncallback=?', {
format: 'json',
method: 'flickr.photos.search',
api_key: '7965a8bc5a2a88908e8321f3f56c80ea',
user_id: '77921082@N00',
per_page: number
}).done(function(data) {
$.each(data.photos.photo, function(i, item) {
_this.photos.push(item);
});
});
};
})();
Sinon无法处理JSONP请求,因为它只是存根XMLHttpRequest对象。问题是JSONP与XMLHttpRequest无关。它只是一个脚本标记,放在DOM中,用返回的数据调用一个全局函数
在本例中,您必须使用stub$.getJSON返回一个stub,该stub将使用结果数据调用回调
var json = {"photos":{"page":1, "pages":726, "perpage":5, "total":"3630", "photo":[{"id":"8591804280", "owner":"77921082@N00", "secret":"da96195b4b", "server":"8526", "farm":9, "title":"Pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591810388", "owner":"77921082@N00", "secret":"d94ce346a5", "server":"8509", "farm":9, "title":"Street Plate", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591801040", "owner":"77921082@N00", "secret":"cb7b1e246a", "server":"8097", "farm":9, "title":"Stone pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590414659", "owner":"77921082@N00", "secret":"fb49a25607", "server":"8094", "farm":9, "title":"Street pole", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590411479", "owner":"77921082@N00", "secret":"9aab17d3a9", "server":"8370", "farm":9, "title":"Street plate", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"};
sinon.stub($, 'getJSON').returns({done: sinon.stub().callsArgWith(0, json)})
您可以覆盖$。getJSON
var backup = $.getJSON;
$.getJSON = function (url, func) {
var response = {
total: 3630
};
func(response);
};
$.getJSON = backup; // restore original implementation