在ember测试中进行异步调用

时间:2014-08-26 17:52:16

标签: testing asynchronous ember.js qunit

在我的ember应用程序中,我使用bootbox(http://bootboxjs.com/)来确认删除某些模型。 在我的qunit测试中,我单击带有{{action}}的按钮,在此操作中我打开一个bootbox。 在文本中,我单击引导框上的“确定”按钮。

但是andThen方法不会等待bootbox消失。因此,检查模型是否被移除总是错误的。我试图回复一个承诺,但是那个andThen帮手也不会等待承诺。

有没有办法让测试等到解决了bootbox的承诺,或者bootbox的回调完成了?

额外信息: 我的模板是:

    <button class="btn btn-default" {{action 'askDelete' target="view"}} data-toggle="tooltip" data-placement="right" {{translateAttr title="trips.remove"}}><i class="fa fa-trash-o"></i></button>

我的观点是:

actions: {
    askDelete: function(){
        var self = this;
        self.$('td').slideUp('500').promise().then(function(){
            self.get('controller').send('delete');
        });
    }
}

我的控制器:

actions: {
    save: function(){
                self.get('content').save().then(function(){
                self.get('controllers.history').transitionToPrevious('trips.index');
            });

我的测试:

click(".specific-row .action-buttons button");

andThen(function() {
    // Check if the row is indeed removed from the table
    ok(find(".content-row table tr:contains('content-of-row')").length === 0, "deleted and removed from the table");
});

但是在触发控制器中的保存操作之前调用最新的ok,因此始终为false。

我在最小的情况下制作了一个jsfiddle: http://jsfiddle.net/21af9puz/1/

1 个答案:

答案 0 :(得分:5)

由于您的进程在ember运行循环之外工作,因此您需要对测试过程进行一些手动管理。这些方面的东西:

test('Test ', function(){
    console.log(find('#link'));
  visit('/');
  andThen(function() {
      equal(find('#block').length,1, 'There should be a block on the page');
      click("#link"); 

      stop();
      Em.run.later(function(){
        start();
        $('.btn.btn-primary').click();
        equal(find('#block').length,0, 'There should be no block on the page');
      }, 1000);


  });
});

http://jsfiddle.net/xke7851k/