当异步(使用ember-mocha-adapter)时,Ember Mocha测试失败 [英] Ember Mocha tests fail when async (using ember-mocha-adapter)

查看:208
本文介绍了当异步(使用ember-mocha-adapter)时,Ember Mocha测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  describe('Location Panel',function(){
beforeEach(function(){
App.reset();
visit('/ map / 41.76721,-72.66907') ;
});

它('有适当的地址',function(){
var $ title = find('。panel-header h2');
expect($ title).to.have.text('476 Columbus Blvd,Hartford');
});
});

基本上找不到任何DOM元素,因为它在路由之前运行测试完成加载..如果我在测试中访问,并使用和Then 等等,同样会发生。 p>

这是一个 jsbin 用于调试。



修改



在jsbin中,我正在使用一个嘲笑的ajax调用,但在我的测试中,ajax调用是真实的。我正在使用 Ember。$。ajax 包装如下:

 函数ajax(url,options){
return new Ember.RSVP.Promise(function(resolve,reject){
options = options || {};
options.url = url;

options.success = function(data){
Ember.run(null,resolve,data);
};

options.error = function (jqxhr,status,something){
Ember.run(null,reject,arguments);
};

Ember。$。ajax(options);
});
}

我应该使用 Ember.run.later 还有?

解决方案

你应该使用 Ember.run.later 而不是 setTimeout ,以便等待帮助者知道它应该等待。



或者,您可以使用 Ember.test.registerWaiter ,尽管我不认为您需要在这里。



更新JSBIN: http://emberjs.jsbin.com/gahe/ 1 /编辑


I can't get mocha working with Ember due to the fact that it fails when a test of the following nature is executed:

describe('Location Panel', function () {
  beforeEach(function () {
    App.reset();
    visit('/map/41.76721,-72.66907');
  });

  it('Have proper address', function () {
    var $title = find('.panel-header h2');
    expect($title).to.have.text('476 Columbus Blvd, Hartford');
  });
});

Basically it can't find any of the DOM elements, because it runs the test before the route has finished loading.. The same happens if I visit from within the test, and use andThen, etc..

Here's a jsbin for debugging.

Edit

In the jsbin, I'm using a mocked ajax call, but in my tests the ajax calls are real. I am using Ember.$.ajax wrapped in the following:

function ajax (url, options) {
  return new Ember.RSVP.Promise(function (resolve, reject) {
    options = options || {};
    options.url = url;

    options.success = function (data) {
      Ember.run(null, resolve, data);
    };

    options.error = function (jqxhr, status, something) {
      Ember.run(null, reject, arguments);
    };

    Ember.$.ajax(options);
  });
}

Should I be using Ember.run.later as well?

解决方案

You should use Ember.run.later instead of setTimeout so that the wait helper knows that it should wait.

Alternatively you can use Ember.test.registerWaiter though I don't think you need it here.

Updated JSBIN: http://emberjs.jsbin.com/gahe/1/edit

这篇关于当异步(使用ember-mocha-adapter)时,Ember Mocha测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆