Ember.JS Integration Testing Issues with andThen,然后单击helpers
Ember.JS Integration Testing Issues with andThen and click helpers
我使用Ember的测试助手andThen
和click
得到了奇怪的结果。根据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');
语句按顺序执行。
相关文章:
- 复选框,然后单击事件处理
- 选择其中一个单选按钮,然后单击“提交”,重定向其他页面
- JS将复选框中的值添加到URL,然后单击转到URL
- 在表中创建带有输入的新行,然后单击保存该输入的值
- 我可以在蜘蛛网的高图表中添加两个轴,然后单击x轴标签导航到另一个页面吗
- 将 Div 类附加到超链接类 — 然后单击时克隆并附加超链接到容器
- Chrome扩展程序获取DOM文本并在弹出窗口中显示.html然后单击按钮
- window.打开一个新页面,然后单击新页面中的链接 - JavaScript
- KNOCKOUTJS 复选框,然后单击父 TD
- 在 ajax 调用中加载日期选择器,然后单击不起作用
- 管理 Shorcut,然后单击 Browser with JQuery
- 创建一个链接,然后单击一个按钮即可单击它
- 从PHP代码加载图像,然后单击时更改主页上的图像
- 查看 json,然后单击该 json 中的 links.json,并将其替换为当前 json 视图
- 从应用程序加载网页,然后单击该页面上的链接
- 单击以打开图像,然后单击以将其关闭
- 鼠标悬停,鼠标退出,然后单击谷歌地图上的同一标记
- React渲染一个组件,然后单击另一个React组件
- Join events按键(带keyCode),然后单击链接
- C#/VB.net,然后单击javascript don'不起作用