我正在学习JavaScript和Jasmine-Node我已经成功通过了前11个匹配器。但我似乎无法通过这个问题来解决这个问题,而且我已经找到了我能想到的所有内容。我被卡住了。我真的很感激一些帮助,对我的代码的一般评论也是受欢迎的。
这是规范测试...
var Triangle = require('./triangle');
describe('Triangle', function() {
it('equilateral triangles have equal sides', function() {
var triangle = new Triangle(2,2,2);
expect(triangle.kind()).toEqual('equilateral');
});
it('larger equilateral triangles also have equal sides', function() {
var triangle = new Triangle(10,10,10);
expect(triangle.kind()).toEqual('equilateral');
});
it('isosceles triangles have last two sides equal', function() {
var triangle = new Triangle(3,4,4);
expect(triangle.kind()).toEqual('isosceles');
});
it('isosceles trianges have first and last sides equal', function() {
var triangle = new Triangle(4,3,4);
expect(triangle.kind()).toEqual('isosceles');
});
it('isosceles triangles have two first sides equal', function() {
var triangle = new Triangle(4,4,3);
expect(triangle.kind()).toEqual('isosceles');
});
it('isosceles triangles have in fact exactly two sides equal', function() {
var triangle = new Triangle(10,10,2);
expect(triangle.kind()).toEqual('isosceles');
});
it('scalene triangles have no equal sides', function() {
var triangle = new Triangle(3,4,5);
expect(triangle.kind()).toEqual('scalene');
});
it('scalene triangles have no equal sides at a larger scale too', function() {
var triangle = new Triangle(10,11,12);
expect(triangle.kind()).toEqual('scalene');
});
it('scalene triangles have no equal sides in descending order either', function() {
var triangle = new Triangle(5,4,2);
expect(triangle.kind()).toEqual('scalene');
});
it('very small triangles are legal', function() {
var triangle = new Triangle(0.4,0.6,0.3);
expect(triangle.kind()).toEqual('scalene');
});
it('test triangles with no size are illegal', function() {
var triangle = new Triangle(0,0,0);
expect(triangle.kind.bind(triangle)).toThrow();
});
xit('triangles with negative sides are illegal', function() {
var triangle = new Triangle(3,4,-5);
expect(triangle.kind.bind(triangle)).toThrow();
});
xit('triangles violating triangle inequality are illegal', function() {
var triangle = new Triangle(1,1,3);
expect(triangle.kind.bind(triangle)).toThrow();
});
xit('edge cases of triangle inequality are in fact legal', function() {
var triangle = new Triangle(2,4,2);
expect(triangle.kind.bind(triangle)).not.toThrow();
});
xit('triangles violating triangle inequality are illegal 2', function() {
var triangle = new Triangle(7,3,2);
expect(triangle.kind.bind(triangle)).toThrow();
});
xit('triangles violating triangle inequality are illegal 3', function() {
var triangle = new Triangle(10,1,3);
expect(triangle.kind.bind(triangle)).toThrow();
});
});
这是我的JavaScript ......
function Triangle(sideA, sideB, sideC) {
this.kind = function() {
//
// Your solution to the exercise goes here
//
var triangleType = "";
if (this.isValidTriangle()){
if (sideA == sideB && sideB == sideC ) {
triangleType="equilateral";
}
else if (sideA == sideB || sideB == sideC || sideA == sideC) {
triangleType="isosceles";
}
else {
triangleType="scalene";
}
}
else {
// throw new Error('Invalid Triangle');
// throw TypeError("Invalid Triangle");
// throw "up";
throw new RangeError("Test");
}
return triangleType;
};
this.isValidTriangle = function() {
var sides = [sideA, sideB, sideC];
for(var i = 0; i < sides.length; i++) {
if(sides[i] = 0) {i = 3; return false;};
};
return true;
};
};
module.exports = Triangle;
这是Jasmine的输出......
c:\Users\EP\exercism\javascript>jasmine-node triangle --verbose
Triangle - 20 ms
equilateral triangles have equal sides - 16 ms
larger equilateral triangles also have equal sides - 1 ms
isosceles triangles have last two sides equal - 0 ms
isosceles trianges have first and last sides equal - 0 ms
isosceles triangles have two first sides equal - 0 ms
isosceles triangles have in fact exactly two sides equal - 0 ms
scalene triangles have no equal sides - 0 ms
scalene triangles have no equal sides at a larger scale too - 0 ms
scalene triangles have no equal sides in descending order either - 0 ms
very small triangles are legal - 0 ms
test triangles with no size are illegal - 1 ms
Failures:
1) Triangle test triangles with no size are illegal
Message:
Expected function to throw an exception.
Stacktrace:
Error: Expected function to throw an exception.
at null.<anonymous> (C:\Users\EP\exercism\javascript\triangle\triangle.spec.js:57:42)
Finished in 0.053 seconds
11 tests, 11 assertions, 1 failure, 0 skipped
我知道我必须遗漏一些东西。我已经阅读过Jasmine doc,JavaScript doc,MDN,用google搜索它,似乎无法确定我做错了什么。
更新:在stackoverflow社区的帮助下,我能够克服这个问题。下面是通过所有匹配器的最终代码(挂起的那些(xit)全部被激活(它)。
function Triangle(sideA, sideB, sideC) {
this.kind = function() {
//
// Your solution to the exercise goes here
//
if (this.isValidTriangle() == false){ throw new TypeError("Invalid Triangle");}
else if (sideA == sideB && sideB == sideC ) {
triangleType="equilateral";
}
else if (sideA == sideB || sideB == sideC || sideA == sideC) {
triangleType="isosceles";
}
else {
triangleType="scalene";
}
return triangleType;
};
this.isValidTriangle = function() {
// no triangle inequality
if(sideA + sideB < sideC) {return false;}
if(sideB + sideC < sideA) {return false;}
if(sideA + sideC < sideB) {return false;}
// no zero or negative sides
var sides = [sideA, sideB, sideC];
for(var i = 0; i < 3; i++) {
if(sides[i] <= 0) {i = 3; return false;};
};
return true;
};
}
module.exports = Triangle;
再次感谢所有帮助我解答的人!
答案 0 :(得分:0)
原因在于您直接调用该方法并且这会创建一个javascript错误,您需要传递一个将由jasmine调用的回调...这允许jasmine捕获您的错误:
describe("Something throws", () => {
it("foo is not defined", () => {
expect(() => { foo + 5 }).toThrow();
// instead of:
// expect(foo + 5).toThrow();
});
});
答案 1 :(得分:0)