Ember.JS集成测试问题与andThen并单击帮助程序

时间:2014-08-27 15:01:05

标签: javascript ember.js integration-testing

我与Ember的测试助手andThenclick得到奇怪的结果。根据Ember的documentation

  

andThen帮助程序将等待所有前面的异步帮助程序   在继续前进之前完成。

但是,我发现这似乎并非总是如此。在下面的示例中,有3个console.debug语句。我希望他们能按照A - &gt;的顺序登录。 B - &gt; C.但我一直得到这个顺序:A - &gt; C - &gt; 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的异步测试助手。其次,我使用自定义andThen帮助程序替换了代码中的所有next帮助程序。这允许我的代码按照我期望的顺序运行。

// 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');
});

2 个答案:

答案 0 :(得分:6)

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');
});

答案 1 :(得分:1)

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

console.debug('mark A');

await click('div:first');
await click('div:first');

console.debug('mark B');

console.debug('mark C');

语句按顺序执行。