【发布时间】:2016-01-05 11:06:41
【问题描述】:
我正在开发一个处理特定类型文档的 CRUD 的应用程序。因此,我们经常希望根据一组值测试该文档的一致性。我有一个内部带有 it() 的 describe() 块,用于测试该文档的每个字段。
我想让它可重用,但简单地围绕该描述块包装一个函数会导致时间问题,并且在该包装函数中的测试之前,以下测试运行得太早。
有没有一种模式可以做到这一点?应该是承诺吗? 我应该把它变成一个匹配器吗? docToBeEqualTo() ?我还没有看到在匹配器中使用 it(),它似乎不是适合这个的地方
我想我在这里缺少一个常见的模式。你能帮忙吗?
非常感谢
编辑:请求的代码示例:
function compareDocs(targetData, timeout){
describe('the doc should contain : ', function(){
it('The amount field should be : "' + targetData.amount + '". ', function(){
expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.amount);
}, timeout);
it('The foo bar number should be : "' + targetData.fooBar + '". ', function(){
expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.fooBarNumber);
}, timeout);
return browser.waitForAngular();
});
});
这样使用:
describe('first describe', function(){
it('compare docs', function(){
compareDocs(currentData,targetData);
}, timeout);
});
describe('second describe where the doc is deleted', function(){
it('should remove payment from list', function(){
deleteDoc();//this runs too early, and delete the doc before compareDocs() has finished
expect(element(by.css(selector)).isPresent).toBe(false);
}, timeout);
});
TL;DR : 如何防止deleteDoc() 在compareDoc() 完成之前运行?
【问题讨论】:
-
分享代码sn-p!
-
不支持将
it或describe嵌套在另一个it块中。its 应该有测试代码和期望;describe- 另一个describe和/或it的容器。
标签: javascript angularjs unit-testing jasmine protractor