我刚开始使用Qunit,想知道是否有办法捕获/验证/省略警报,例如:
function to_test() {
alert("I'm displaying an alert");
return 42;
}
Run Code Online (Sandbox Code Playgroud)
然后有类似的东西:
test("to_test", function() {
//in this case I'd like to test the alert.
alerts("I'm displaying an alert", to_test(), "to_test() should display an alert");
equals(42, to_test(), "to_test() should return 42" ); // in this case I'd like to omit the alert
});
Run Code Online (Sandbox Code Playgroud)
我对使用其他单元测试工具的建议持开放态度.
提前致谢!
我已经安装了JsUnit和一个测试用例如下:
/home/chernevik/Programming/JavaScript/jsunit
/home/chernevik/Programming/JavaScript/jsunit/testRunner.html
/home/chernevik/Programming/JavaScript/jsunit/myTests/lineTestAbs.html
/home/chernevik/Programming/JavaScript/lineTestAbs.html
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中将测试运行器作为文件打开,并从jsunit/myTests目录测试lineTestAbs.html时,它会通过.当我从JavaScript目录测试同一文件时,测试运行器超时,询问文件是否存在或是否为测试页.
问题:我在这里做错了什么,或者这是预期的行为?是否可以将测试用例放在不同的目录结构中,如果是这样,对JsUnitCore.js的正确路径引用是什么?如果从HTTP服务器检索文件,JsUnit会有不同的行为吗?
<html>
<head>
<title>Test Page line(m, x, b)</title>
<script language="JavaScript" src="/home/chernevik/Programming/JavaScript/jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript">
function line(m, x, b) {
return m*x + b;
}
function testCalculationIsValid() {
assertEquals("zero intercept", 10, line(5, 2, 0));
assertEquals("zero slope", 5, line(0, 2, 5));
assertEquals("at x = 10", 25, line(2, 10, 5));
}
</script>
</head>
<body>
This pages tests line(m, x, b).
</body>
</html>
Run Code Online (Sandbox Code Playgroud)