我从Javascript单元测试中设置了基本示例(第1章)并且它可以工作,例如
为什么,当我为第2章设置一个新的目录时,看起来有相同的方式,但是我自己的规范却没有运行规范:
我的跑步者档案有:
<script type="text/javascript" src="src/SimpleMath.js"></script>
<script type="text/javascript" src="spec/SimpleMathSpec.js"></script>
我的规范文件是
spec/SimpleMathSpec.js
我的源文件是
src/SimpleMath.js
它们都存在:
$ ls src/SimpleMath.js
src/SimpleMath.js
10:56:21 durrantm U2017 /home/durrantm/Dropbox/_/jasmine_2017/Chapter2
$ ls spec/SimpleMathSpec.js
spec/SimpleMathSpec.js
网络标签显示它们被发现正常 - 虽然不确定为什么零尺寸???
内容:
spec/SimpleMathSpec.js:
describe("SimpleMath", function() {
var simpleMath;
beforeEach(function() {
simpleMath = new SimpleMath();
});
it("should calculate a factorial for a positive number", function() {
result=simpleMath.getFactorial(3);
expect(result).toEqual(6);
});
it("should calculate a factorial for 0 - which will be zero", function() {
result=simpleMath.getFactorial(0);
expect(result).toEqual(0);
});
it("should calculate a factorial for -3 - which will raise an error", function() {
result=simpleMath.getFactorial(-3);
expect(result).toThrow Error;
});
});
src/SimpleMath.js:
SimpleMath = function() {};
SimpleMath.prototype.getFactorial = function(number) {
if (number < 0) {
throw new Error("Can't be less than zero");
}
else if (number == 1 || number == 0) {
return 1;
}
else {
return number * this.getFactorial(number-1);
}
}
SimpleMath.prototype.signum = function(number) {
if (number > 0) {
return 1;
} else if (number == 0) {
return 0;
} else {
return -1;
}
}
SimpleMath.prototype.average = function(number1, number2) {
return (number1 + number2) / 2;
}
答案 0 :(得分:0)
这是语法错误:
expect(result).toThrow Error;
应该是
expect(result).toThrowError;
语法错误显示在Web检查工具控制台中,并阻止任何规范运行。