我跟随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调用不会尝试使用模拟服务。为什么呢?
答案 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之间进行转换的适配器(如果需要使用它)使用后者操作的小部件。