我正在为 Ember 2.16 组件编写集成测试,并且正在测试一些用户操作。
用户操作之一调用 window.confirm()
,在删除项目之前询问用户是否确定要删除该项目。
我想测试该组件的功能,包括接受和拒绝确认。组件操作类似于:
delete(id){
if(confirm('Are you sure you want to delete?')){
//do stuff
} else {
//do other stuff
}
}
在我的集成测试中,我成功单击按钮以弹出提示,但遇到了此错误:
[Testem] Calling window.confirm() in tests is disabled, because it causes testem to fail with browser disconnect error.
如何创建绕过 window.confirm()
的集成测试功能?
我在我的组件中添加了一种绕过确认环境是否处于“测试”模式的方法,但这并没有真正帮助,因为我没有测试依赖于 window.confirm()
的代码部分。 .
我环顾四周,看看是否有一个变量可以传递给组件来制作 window.confirm()
真/假,但没有成功。
如何创建一个测试来测试调用 window.confirm()
的组件在一个 Action 中?
最佳答案
一种解决方案是保存 window.confirm
的原始实现并在测试之前编写自己的实现,然后在测试结束时恢复原始实现。
这就是我要做的:
// Watch out, this test is written with the latest ember-qunit syntax which might not be exactly what you have in your Ember 2.16 application
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from 'ember-test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('your component integration tests', function(hooks) {
setupRenderingTest(hooks);
test('clicking the OK confirm button', async function(assert) {
// save the original window.confirm implementation
const originalWindowConfirm = window.confirm;
// simulate the OK button clicked
window.confirm = function() { return true;}
// ADD YOUR TEST AND ASSERTIONS HERE
// restore the original window.confirm implementation
window.confirm = originalWindowConfirm;
});
});
关于javascript - 在 Ember 2.16 中创建利用 window.confirm() 的集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441106/