Ember Mocha tests fail when async (using ember-mocha-adapter)

久未见 提交于 2020-01-05 12:07:29

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21630048/ember-mocha-tests-fail-when-async-using-ember-mocha-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!