0

我的 DOH 框架由 nodejs 运行。(版本为 1.10)

我知道 nodejs 应该使用xmlhttprequest或其他模块来完成 XHR 请求。就我而言,我不是直接使用 nodejs XHR,而是使用 dojo 的 xhr。显然,dojo 的 xhr 不能由 nodejs 运行。因为 nodejs 不应该能够在没有 npm 模块的情况下运行 XHR。

有没有可能解决这个问题?

命令:

node unittest.js load=doh test=test_custom_ajax.js

单元测试.js:

global.dojoConfig = {
  baseUrl: ".",
  packages: [
    { name: "dojo", location: "./dojo" },
    { name: "dojox", location: "./dojox" },
    { name: "dijit", location: "./dijit" },
    { name: "my", location: "./my" }
  ]
}
require("./dojo/dojo.js");

custom_ajax.js:

define([
  "dojo",
  "dojo/_base/declare",
  "dojo/_base/lang",
  "dojo/request/xhr"
], function(dojo, declare, lang, xhr) {

  return declare("my.custom_ajax", null, {

    ajaxGet: function(url, options) {
      options = options || {};

      return xhr(url, {
        method: "GET",
        handleAs: "json",
        headers: {
          "Content-Type":"application/json",
          "Accept":"application/json"
        },
      }).then(
        lang.hitch(this, function(data) { 
            /* handle data */
            if (options.onHandle) {options.onHandle(resp)}
            if (options.onLoad) {options.onLoad(resp)}
            if (options.onComplete) {options.onComplete(resp)}
        }),
        lang.hitch(this, function(err) {
            /* handle error */
            if (options.onError) {options.onError(resp)}
        })
      );
    }
  });
});

test_custom_ajax.js:

define([
  "doh/runner",
  "my/custom_ajax"
], function(doh, custom_ajax) {
  doh.register("test_custom_ajax", [{
    var ca = new custom_ajax();
    var options = {};
    ca.ajaxGet('some_url', options);
  }]);
}

结果:

Error: XMLHTTP not available
4

1 回答 1

0

这是最终解决方案:加载 Ajax 之前的存根 xhr

test_custom_ajax.js

define([
  "doh/runner",
  "dojo/node!sinon",
  "dojo/request/xhr",
  "dojo/Deferred"
], function(doh, sinon, xhr, Deferred) {

  doh.register("test_custom_ajax",[{

/*--------------------------------------------------
              stub xhr before load Ajax
--------------------------------------------------*/
    // original xhr -> custom xhr
    var oldXhr = require.modules["dojo/request/xhr"]["result"];
    var getDeferred;
      require.modules["dojo/request/xhr"]["result"] = function(url, options) {
        if (options.method === 'GET') {
          getDeferred = new Deferred();
          return getDeferred;
        }
      };
      var Ajax;
      require(["my/custom_ajax"], function(custom_ajax) {
        Ajax = custom_ajax;
        // custom xhr -> original xhr 
        require.modules["dojo/request/xhr"]["result"] = oldXhr;
      });

      // prepare stub
      var onHandleStub = sinon.stub();
      var onLoadStub = sinon.stub();
      var onCompleteStub = sinon.stub();
      var onErrorStub = sinon.stub();

/*------------------------------------------------*/

      // Inside this new Ajax, xhr is customized
      var ca = new Ajax();

      // Normal AjaxGet
      ca.ajaxGet('some_url', {
        onHandle: onHandleStub,
        onLoad: onLoadStub,
        onComplete: onCompleteStub
      });
      getDeferred.resolve(); //control xhr
      /* assert stub */

      // Error AjaxGet
      ca.ajaxGet('some_url', {
        onError: onErrorStub,
      });
      getDeferred.reject(); //control xhr
      /* assert stub */
    }]
  );
}
于 2016-10-20T11:52:54.357 回答