在茉莉花2中处理异步测试的更好方法
A better way to handle async tests in jasmine 2
这是我的三个测试中的一个例子…
describe('Enabled button (no disabled attribute)', function () {
var el, clicked = false;
beforeEach(function (done) {
// Create the tag
el = document.createElement('rui-button')
el.innerHTML = 'Test Button';
document.body.appendChild(el)
el.addEventListener('click', function () {
clicked = true;
done();
});
// Mount the tag
tag = riot.mount('rui-button')[0]
expect(tag).toBeDefined();
expect(tag.isMounted).toBe(true);
tag.update();
el.childNodes[0].click();
});
it('should be disabled', function () {
expect(el.getAttribute('data-disabled')).toEqual('false');
expect(clicked).toEqual(true);
var style = window.getComputedStyle(el.childNodes[0]);
expect(style.cursor).toEqual('pointer');
expect(style.opacity).toEqual('1')
});
});
describe('Enabled button (disabled="{ false }")', function () {
var el, clicked = false;
beforeEach(function (done) {
// Create the tag
el = document.createElement('rui-button')
el.innerHTML = 'Test Button';
el.setAttribute('disabled', '{ false }');
document.body.appendChild(el)
el.addEventListener('click', function () {
clicked = true;
done();
});
// Mount the tag
tag = riot.mount('rui-button')[0]
expect(tag).toBeDefined();
expect(tag.isMounted).toBe(true);
tag.update();
el.childNodes[0].click();
});
it('should be disabled', function () {
expect(el.getAttribute('data-disabled')).toEqual('false');
expect(clicked).toEqual(true);
var style = window.getComputedStyle(el.childNodes[0]);
expect(style.cursor).toEqual('pointer');
expect(style.opacity).toEqual('1')
});
});
describe('Disabled button (disabled)', function () {
var el, clicked = false;
beforeEach(function (done) {
// Create the tag
el = document.createElement('rui-button')
el.innerHTML = 'Test Button';
el.setAttribute('disabled', '');
document.body.appendChild(el)
el.addEventListener('click', function () {
clicked = true;
done();
});
// Mount the tag
tag = riot.mount('rui-button')[0]
expect(tag).toBeDefined();
expect(tag.isMounted).toBe(true);
tag.update();
el.childNodes[0].click();
});
it('should be disabled', function () {
expect(el.getAttribute('data-disabled')).toEqual('true');
expect(clicked).toEqual(false);
var style = window.getComputedStyle(el.childNodes[0]);
expect(style.cursor).toEqual('not-allowed');
expect(style.opacity).not.toEqual('1')
});
});
您可以看到,每个测试都由describe
, beforeEach
和it
组成。当测试失败时,我得到一些无用的超时错误。
下面是测试工作时我得到的结果:
Enabled button (no disabled attribute)
✓ should be enabled
Enabled button (disabled="{ false }")
✓ should be enabled
Disabled button (disabled)
✗ should be disabled
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
无论如何都失败了,测试通过了,但是超时导致了失败。我在测试click事件是否没有发生。它有6行,每个测试两行。我真的更喜欢这样:
✓ Enabled button (no disabled attribute)
✓ Enabled button (disabled="{ false }")
✓ Disabled button (disabled)
我需要有负测试工作,测试缺乏点击。
当测试确实失败时,我得到这个,相同的超时crud以及有效的失败原因。
✗ should be enabled
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Expected false to equal true.
at Object.<anonymous> (ui-button.spec.js:89:23)
是否有一些方法来测试缺少事件?有没有其他更好的方式,我可以写我的规格文件?我在这里只列出了3个测试,但我有更多。
您有两个选择。
- 更简单的是添加负测试来描述没有
beforeEach
的地方,在那里单击处理程序执行done()
回调。你显然不能点击禁用的HTML元素。
2。例如,像这样更改beforeEach
(关键是在beforeEach
的某个点执行done()
回调):
beforeEach(function (done) {
// Create the tag
el = document.createElement('rui-button')
el.innerHTML = 'Test Button';
el.setAttribute('disabled', '');
document.body.appendChild(el)
el.addEventListener('click', function () {
clicked = true;
done();
});
// Mount the tag
tag = riot.mount('rui-button')[0]
expect(tag).toBeDefined();
expect(tag.isMounted).toBe(true);
tag.update();
if (el.getAttribute('data-disabled')) {
done();
} else {
el.childNodes[0].click();
}
});
相关文章:
- 有没有更好的动手、具体的方法来学习Javascript
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 在JavaScript中拆分日期字符串的更好方法是什么
- 为什么$.brower被弃用?还有什么更好的替代方案
- 设置嵌套对象属性的更好方法
- 您有更好的动态方式来缩短复杂的代码jquery吗
- 用Javascript重新格式化复杂文本日期字符串的更好方法
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 动态更改测试中代码覆盖率的require语句的更好方法
- 什么是上下文中静态类型javascript的示例,以更好地进行单元测试
- 更好的是:测试事件处理程序是否绑定或直接使用 .off()
- 在测试JavaScript时,有比setTimeout更好的方法来等待asnyc回调
- 如何在控制台中获得更好的测试报告
- Nightwatch:比.pause(1000)更好的方法来避免脆弱的测试
- 在茉莉花2中处理异步测试的更好方法
- 使因果报应测试看起来更好(或者更像rspec)
- Regex测试,有更好的方法写这个吗
- 测试对象中是否存在嵌套属性的更好方法
- 一个更好的测试base64 URI支持(我可以在JS中创建一个大的base64编码的图像吗?)
- Node.js如何使用mocha获得更好的异步测试错误消息