Ember.JS Integration Testing Issues with andThen,然后单击helpers

Ember.JS Integration Testing Issues with andThen and click helpers

本文关键字:然后 单击 helpers andThen with JS Integration Testing Issues Ember      更新时间:2023-09-26

我使用Ember的测试助手andThenclick得到了奇怪的结果。根据Ember的文件:

andThen助手将等待所有前面的异步助手在前进之前完成。

然而,我发现情况并非总是如此。在下面的示例中,有3个console.debug语句。我希望他们以A->B->C的顺序登录。但我一直得到这个顺序:A->C->B。当我只使用两个点击助手中的一个时,我只能得到预期的ABC订单。没有与单击助手中引用的<div>元素相关联的事件侦听器(操作)。

有人能解释这种出乎意料的行为吗?我对助手的使用有错误吗?或者是Ember测试框架的错误?

andThen(function() {
    console.debug('mark A');
    click('div:first'); // with just 1 click helper, debug order is ABC
    click('div:first'); // with this second click helper, debug order is ACB
    andThen(function() {
        console.debug('mark B');
    });
});
andThen(function() {
    console.debug('mark C');
});

编辑

根据Kingpin2k给出的答案,我最终采用了以下解决方案来达到我所寻求的测试风格。

首先,我创建了一个名为next的异步测试助手。其次,我用自定义的next助手替换了代码中的所有andThen助手。这使我的代码能够按预期的顺序运行。

// test-helper.js
Ember.Test.registerAsyncHelper('next', function(app, fn) {
    fn();
});
// my-integration-test.js
next(function() {
    console.debug('mark A');
    click('div:first');
    click('div:first');
    next(function() {
        console.debug('mark B');
    });
});
next(function() {
    console.debug('mark C');
});

andThen只是lastPromiseEmberSawCreated.then的语法糖,所以实际上它看起来是这样的:

lastPromiseEmberSawCreated.then(function(){
  console.debug('mark A');
    click('div:first'); // with just 1 click helper, debug order is ABC
    click('div:first'); // with this second click helper, debug order is ACB
  nextLastPromiseEmberSawCreated.then(function() {
        console.debug('mark B');
    });
});
// the promise won't have changed in between these two `andThen` calls 
// because Ember had no cpu time, and you didn't make any additional calls 
lastPromiseEmberSawCreated.then(function(){
    console.debug('mark C');
});

在测试中不再有理由使用andThen,这有望减少混乱。您可以重写您的示例(假设这是在一个标记为async:的函数中

console.debug('mark A');
await click('div:first');
await click('div:first');
console.debug('mark B');
console.debug('mark C');

语句按顺序执行。