实习生测试不在dojo中使用已配置的注册表requestProvider

时间:2015-01-28 03:00:09

标签: dojo intern

我跟随this article在dojo中模拟响应。

我的嘲讽非常类似于文章中的那个:

registry.register(/\/testIntern/, function (url, options) {
  return when({
                value: "Hello World"
            });

根据我的理解,这应该映射到地址上包含“/ testIntern”的任何请求。

我的测试用例非常简单:

// similar to example
var testRest= new Rest("/testIntern", true);
            testRest("").then(lang.hitch(this, function (data) {
                assert.deepEqual("Hello World", data.value, "Expected 'Hello World', but got" + data.value);
            }));

这真的应该很简单。但是当我运行这个测试时,我得到了404 Not Found。看起来测试中的REST调用不会尝试使用模拟服务。为什么呢?

1 个答案:

答案 0 :(得分:0)

您通常认为注册包含dojo/request/registry的网址时,通过dojo/request通过您的处理程序传递任何引用该网址的内容,这是正确的。

不幸的是,dojo/store/JsonRest使用直接使用dojo/_base/xhr的{​​{1}}模块,而不是dojo/request/xhr。使用dojo/request创建的任何注册(以及dojo/request/registry的任何设置)都将遗失在JsonRest上。

您可能希望查看dstore - 其Rest存储实现与defaultProvider相同的服务器请求,但它使用dojo/store/JsonRest而不是硬编码到特定提供程序。 (dojo/request无论如何都会在浏览器中默认为dojo/request,但可以通过dojo/request/xhr覆盖。)dstore包含用于在dstore的API和dojo / store API之间进行转换的适配器(如果需要使用它)使用后者操作的小部件。