Ember测试中的异步调用

4
在我的Ember应用程序中,我使用bootbox(http://bootboxjs.com/)来确认删除某些模型。 在我的qunit测试中,我通过{{ action }}点击按钮,在此操作中打开一个bootbox。 在文本中,我单击bootbox上的OK按钮。
但是,andThen方法不会等待bootbox消失。因此,检查模型是否已删除始终为false。我尝试返回promise,但是andThen助手也不等待promise。
有没有办法让测试等待bootbox的promise被解决,或者等待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/

我发现了一种显示模态框的替代方法:http://jsfiddle.net/21af9puz/14/ - Lrdv
1个回答

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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接