Ember.js 使用sinon和mocha在ember组件中测试ajax行为时出现超时错误

Ember.js 使用sinon和mocha在ember组件中测试ajax行为时出现超时错误,ember.js,ember-data,mocha.js,sinon,Ember.js,Ember Data,Mocha.js,Sinon,我正试着用摩卡和锡诺测试余烬组件。我想通过使用sinon的“useFakeXMLHttpRequest”测试发出ajax调用的组件的一个操作。但此测试会导致超时错误。我使用的是mocha test adapter for ember,取自,我在云中找不到js文件,所以我粘贴了整个代码,所以jsbin中看起来可能有点凌乱 以下是该问题的jsbin链接: 组件的代码为: AS.QuestionViewComponent = Ember.Component.extend({

我正试着用摩卡和锡诺测试余烬组件。我想通过使用sinon的“useFakeXMLHttpRequest”测试发出ajax调用的组件的一个操作。但此测试会导致超时错误。我使用的是mocha test adapter for ember,取自,我在云中找不到js文件,所以我粘贴了整个代码,所以jsbin中看起来可能有点凌乱

以下是该问题的jsbin链接:

组件的代码为:

        AS.QuestionViewComponent = Ember.Component.extend({
            templateName: "components/question-view",
            actions: {
                makeAjaxCall: function() {
                    jQuery.ajax({
                        url: "/todo/items",
                        success: function(data) {

                            //callback(null, data);
                        }
                    });
                }
            }

        });
<a {{action "makeAjaxCall"}} class="test-link">Make ajax call</a>   
与构件关联的把手是:

        AS.QuestionViewComponent = Ember.Component.extend({
            templateName: "components/question-view",
            actions: {
                makeAjaxCall: function() {
                    jQuery.ajax({
                        url: "/todo/items",
                        success: function(data) {

                            //callback(null, data);
                        }
                    });
                }
            }

        });
<a {{action "makeAjaxCall"}} class="test-link">Make ajax call</a>   

非常感谢你的帮助。谢谢,很可能是因为您没有响应伪造的ajax请求。
ember测试
包统计jQuery()发出的未决ajax请求。如果该值保持为1,则余烬测试
wait()
helper将永远不会解析

ember测试
程序包通过以下方式增加此计数器

为了澄清这里发生了什么:当您使用
click()
helper时,会向元素发送一条click消息,然后遵从
wait()
helper
(承诺)。这同样适用于其他帮助程序,如
fillIn()
keyEvent()
等。您可以从
wait()
源代码中的注释中看到,它不会继续处理其余的规范:

// 1. If the router is loading
// 2. *If there are pending Ajax requests
// 3. If there are scheduled timers or we are inside of a run loop
修复方法:

不幸的是,如果您从未到达测试的
then
块,则无法通过
请求[0]伪造响应。响应(…)

相反,我通过使用sinon的假服务器解决了这个问题:

var server;

beforeEach(function () {
  server = sinon.fakeServer.create();
  server.autoRespond = true;
  server.autoRespondAfter = 1; // ms
  App.reset();
});

afterEach(function () {
  server.restore();
});

it("should make ajax call", function() {
  // set up the fake response
  server.responses[0].response = [200, { "Content-Type": "application/json" }, '{ "todos": [] }'];

  visit('/')
  .click($("a.test-link:first"))
  .then(function() {
    // should make it to here now
  });
});
当您希望一个或一个确定的ajax请求顺序进入您的伪服务器时,这种模式可以很好地工作。如果需要大量请求(使用不同的路径),可以使用
server.respondWith([regex],…)
将特定URL与特定响应相匹配

另一件需要注意的事情是,将ajax调用的成功部分放入Ember.run通常是一种好的做法:

jQuery.ajax({
  url: "/todo/items",
  success: function(data) {
    Ember.run(function () {
      //callback(null, data);
    })
  }
});

很可能是因为您没有响应伪造的ajax请求。
ember测试
包统计jQuery()发出的未决ajax请求。如果该值保持为1,则余烬测试
wait()
helper将永远不会解析

ember测试
程序包通过以下方式增加此计数器

为了澄清这里发生了什么:当您使用
click()
helper时,会向元素发送一条click消息,然后遵从
wait()
helper
(承诺)。这同样适用于其他帮助程序,如
fillIn()
keyEvent()
等。您可以从
wait()
源代码中的注释中看到,它不会继续处理其余的规范:

// 1. If the router is loading
// 2. *If there are pending Ajax requests
// 3. If there are scheduled timers or we are inside of a run loop
修复方法:

不幸的是,如果您从未到达测试的
then
块,则无法通过
请求[0]伪造响应。响应(…)

相反,我通过使用sinon的假服务器解决了这个问题:

var server;

beforeEach(function () {
  server = sinon.fakeServer.create();
  server.autoRespond = true;
  server.autoRespondAfter = 1; // ms
  App.reset();
});

afterEach(function () {
  server.restore();
});

it("should make ajax call", function() {
  // set up the fake response
  server.responses[0].response = [200, { "Content-Type": "application/json" }, '{ "todos": [] }'];

  visit('/')
  .click($("a.test-link:first"))
  .then(function() {
    // should make it to here now
  });
});
当您希望一个或一个确定的ajax请求顺序进入您的伪服务器时,这种模式可以很好地工作。如果需要大量请求(使用不同的路径),可以使用
server.respondWith([regex],…)
将特定URL与特定响应相匹配

另一件需要注意的事情是,将ajax调用的成功部分放入Ember.run通常是一种好的做法:

jQuery.ajax({
  url: "/todo/items",
  success: function(data) {
    Ember.run(function () {
      //callback(null, data);
    })
  }
});

谢谢蒂姆的详细回复。我会试试你的修复方法,让你知道它是如何进行的。再次感谢。很抱歉,我花了很长时间才回到这个问题上,但非常感谢。在ie8中,我们仍然会收到这些随机超时错误,但在将此设置为更大的值之后。超时(以秒为单位的时间)解决了问题。感谢Tim提供的详细响应。我会试试你的修复方法,让你知道它是如何进行的。再次感谢。很抱歉,我花了很长时间才回到这个问题上,但非常感谢。在ie8中,我们仍然会遇到这些随机超时错误,但是在将这个.timeout(以秒为单位的时间)设置为较大的值后,它解决了这个问题。