您可以扩展您的 Test 类以添加实用程序方法并避免 DRY 违规。本指南帖子总结了它:https ://www.bryntum.com/docs/siesta/#!/guide/extending_test_class
让我们创建 2 个特殊的断言,它们将检查传递的数字的奇校验。通常,断言需要检查其语句并使用 {@link Siesta.Test#pass} 或 {@link Siesta.Test#fail} 方法报告结果。
Class('MyProject.MyTestClass', {
isa : Siesta.Test.ExtJS,
methods : {
isOdd : function (number, description) {
if (number % 2) {
this.pass(description);
} else {
this.fail(description, {
assertionName : 'isOdd',
got : number,
annotation : 'Need odd number'
});
}
},
isEven : function (number, description) {
if (!(number % 2)) {
this.pass(description);
} else {
this.fail(description, {
assertionName : 'isEven',
got : number,
annotation : 'Need even number'
});
}
}
}
})
失败时,尝试提供尽可能多的有关失败的信息,并以可读的形式格式化失败消息。有关其他选项,请参阅 {@link Siesta.Test#fail} 方法文档。
要使 Harness 使用您的新测试类,您必须通过设置 {@link Siesta.Harness#testClass} 配置选项来指定要使用的测试类:
harness.configure({
title : 'Awesome Test Suite',
testClass : MyProject.MyTestClass,
preload : [
...
]
})
应该在 siesta-all.js 文件之后立即加载测试类:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="__path_to_siesta__/resources/css/siesta-all.css">
<script type="text/javascript" src="__path_to_siesta__/siesta-all.js"></script>
<!-- The file with new test class -->
<script type="text/javascript" src="lib/MyTestClass.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
</body>
</html>
现在您可以在所有测试中使用自定义断言或实用方法:
describe('My test', function(t) {
var nbr = 1;
t.isEven(nbr); // Will fail
})