javascript - correct usage of sinon's fake XMLHttpRequest -


i creating xmlhttprequest javascript module json data server. here code:

(function() {   var makerequest = function(url,callback,opt) {     var xhr;     if (xmlhttprequest) { // mozilla, safari, ...       xhr = new xmlhttprequest();     } else if (activexobject) { // ie       try {         xhr = new activexobject("msxml2.xmlhttp");       }       catch (e) {         try {           xhr = new activexobject("microsoft.xmlhttp");         }         catch (e) {}       }     }      if (!xhr) {       callback.call(this,         'giving :( cannot create xmlhttp instance',         null);       return false;     }     xhr.onreadystatechange = function() {       if (xhr.readystate === 4) {         if (xhr.status === 200) {           var data = xhr.responsetext;           if(opt && !opt.raw) {             try {               data = json.parse(data);             } catch (e) {               callback.call(this, e,null);               return;             }           }           callback.call(this,null,data);         } else {           callback.call(this,             'there problem request.',             null);         }       }     };     var params = '';     if (opt && opt.params && typeof(opt.params) == 'object') {       for( var key in opt.params) {         params += encodeuricomponent(opt.params[key]);       }     }     var method = opt && opt.method ? opt.method : 'get';     if (method == 'get') {       url = params.length > 0 ? url+'?'+params : url;       xhr.open('get', url);       xhr.send();     } else if (method == 'post') {       var data = opt && opt.data ? opt.data : params;       xhr.open('post', url);       xhr.send(json.stringify(data));     }     return xhr;   }    if(typeof module !== 'undefined' && module.exports) {     module.exports = makerequest;   }   if(typeof window!== 'undefined') {     window.getjsondata = makerequest;   } })(); 

now writing test case on nodejs mocha , sinon. using sinon's fakexmlhttprequest test module , test code here:

var expect = require('chai').expect,   getjson = require('../'),   sinon = require('sinon');  describe('get-json-data test request', function() {   beforeeach(function() {     this.xhr = sinon.usefakexmlhttprequest();     var requests = this.requests = [];      this.xhr.oncreate = function (xhr) {       requests.push(xhr);     };   });   aftereach(function() {     this.xhr.restore();   });    it('get json data', function() {     var callback = sinon.spy();     getjson('/some/json', callback);     expect(this.requests.length).to.equal(1);     this.requests[0].respond(200,       {"content-type": "application/json"},       '{"id": 1, "name": "foo"}');     sinon.assert.calledwith(callback, {"id": 1, "name": "foo"});   }); }); 

on running test error:

referenceerror: xmlhttprequest not defined

and seems correct there no xmlhttprequest class/function in nodejs. sinon's fakexmlhttprequest not supposed that. thought in sinon's setup (mocha's beforeeach) replacing native xmlhttprequest fakexmlhttprequest. please suggest doing wrong? or correct way test module @ nodejs?

because running outside of browser environment there no xmlhttprequest object. since mocking sinon can declare fake global function in beforeeach call.

global.xmlhttprequest = sinon.usefakexmlhttprequest(); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -