Javascript 茉莉花Tdd第一次尝试
我正在写一个程序来测试不同的三角形类型,我被卡住了。这是我测试的内容: (为思考过程留下评论) app-spec.jsJavascript 茉莉花Tdd第一次尝试,javascript,jasmine,tdd,Javascript,Jasmine,Tdd,我正在写一个程序来测试不同的三角形类型,我被卡住了。这是我测试的内容: (为思考过程留下评论) app-spec.js describe("Answer", function () { var equilateral; var sideA; var sideB; var sideC; it("Should return equilateral when all sides are equal", function () { expect(w
describe("Answer", function () {
var equilateral;
var sideA;
var sideB;
var sideC;
it("Should return equilateral when all sides are equal", function () {
expect(whichTriangle(sideA, sideB, sideC).toEqual(sideA, sideB, sideC));
// expect (sideA).tobe(sideB);
// expect (sideB).tobe(sideC);
// expect (sideC).tobe(sideA);
// !expect (sideA, sideB, sideC).tobe(undefined);
});
it("Should return isosceles when 2 sides are equal", function () {
// let sideD: number;
// let sideE: number;
// let sideF: number;
expect(sideA).tobe((sideB));
expect(sideB).tobe((sideC));
!expect(sideA, sideB, sideC).toEqual(sideA, sideB, sideC);
});
it("Should return scalene when no sides are equal", function () {
// let sideG: number;
// let sideH: number;
// let sideI: number;
!expect(sideA).tobe(sideB);
!expect(sideB).tobe(sideC);
!expect(sideC).tobe(sideA);
});
});
describe("Isosceles triangle", function () {
var isosceles;
});
这是我的app.js文件的内容:
app.js
function whichTriangle(sideA, sideB, sideC) {
// let side1 = document.getElementById('sideA').value();
// let side2 = document.getElementById('sideB').value();
// let side3 = document.getElementById('sideC').value();
if (side1 === side2 && side3) {
console.log("triangle is Equilateral");
}
else if (side1 === side2 && side1 != side3) {
console.log("triangle is an isosceles");
}
else if (side1 != side2 && side1 != side3 && side2 != side3) {
console.log('triangle is a scalene');
}
else if (side1 === null || side1 === undefined ||
side2 === null || side2 === undefined ||
side3 === null || side3 === undefined) {
console.log('You must enter a number for all 3 sides');
}
else {
console.log('You got me I don\'t know what this shape is. Maybe a rectangle?');
}
}
equalSides(1, 1, 1);
这是我运行应用程序时得到的结果:
茉莉花跑步者
3 specs, 3 failures
Spec List | Failures
Answer Should return equilateral when all sides are equal
TypeError: Cannot read property 'value' of null
TypeError: Cannot read property 'value' of null
at equalSides (http://localhost:63342/greater%20sum%20test/src/app.js:2:49)
at Object.<anonymous> (http://localhost:63342/greater%20sum%20test/spec/app-spec.js:7:16)
at attemptSync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
at QueueRunner.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
at Spec.queueRunnerFactory (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
at Spec.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
at attemptAsync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
Answer Should return isosceles when 2 sides are equal
TypeError: expect(...).tobe is not a function
TypeError: expect(...).tobe is not a function
at Object.<anonymous> (http://localhost:63342/greater%20sum%20test/spec/app-spec.js:17:23)
at attemptSync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
at QueueRunner.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
at Spec.queueRunnerFactory (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
at Spec.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
at attemptAsync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
at http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3924:18
Answer Should return scalene when no sides are equal
TypeError: expect(...).tobe is not a function
TypeError: expect(...).tobe is not a function
at Object.<anonymous> (http://localhost:63342/greater%20sum%20test/spec/app-spec.js:25:24)
at attemptSync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
at QueueRunner.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
at Spec.queueRunnerFactory (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
at Spec.execute (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
at attemptAsync (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
at QueueRunner.run (http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
at http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3924:18
3个规格,3个故障
规格清单|故障
当所有边都相等时,答案应返回等边
TypeError:无法读取null的属性“值”
TypeError:无法读取null的属性“值”
在equalSides(http://localhost:63342/greater%20sum%20test/src/app.js:2:49)
反对。(http://localhost:63342/greater%20sum%20test/spec/app-规范js:7:16)
尝试同步(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
在QueueRunner.execute(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
在指定的工厂(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
按规定执行(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
异步尝试(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
当两条边相等时,答案应返回等腰线
TypeError:expect(…)。tobe不是函数
TypeError:expect(…)。tobe不是函数
反对。(http://localhost:63342/greater%20sum%20test/spec/app-规范js:17:23)
尝试同步(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
在QueueRunner.execute(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
在指定的工厂(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
按规定执行(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
异步尝试(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
在http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3924:18
当没有边相等时,答案应返回不等边
TypeError:expect(…)。tobe不是函数
TypeError:expect(…)。tobe不是函数
反对。(http://localhost:63342/greater%20sum%20test/spec/app-规范js:25:24)
尝试同步(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3898:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3887:9)
在QueueRunner.execute(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3872:10)
在指定的工厂(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:816:35)
按规定执行(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:473:10)
at Object.fn(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:4975:37)
异步尝试(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3945:24)
在QueueRunner.run(http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3884:9)
在http://localhost:63342/greater%20sum%20test/lib/jasmine-2.6.4/jasmine.js:3924:18
当然,我知道我做错了什么,但任何朝着正确方向的推动都是非常值得赞赏的,批评也是受欢迎的。必须是
才能成为而不是才能成为。我还注意到你试图用来说而不是代码>这不起作用。你需要做expect(某事)。not.toBe(某事)
看起来您已经知道前两个错误(文档选择器)的原因
这些就是代码抛出错误的原因。尽管您的单元测试和要测试的功能需要仔细重新考虑。您的单位描述函数的返回值应该是一个描述三角形的字符串。但是,您的函数不会返回字符串,而是打印到控制台。如果函数返回“等边”、“等腰”或“不等边”,则效果更好
function whichTriangle(sideA, sideB, sideC) {
// let side1 = document.getElementById('sideA').value();
// let side2 = document.getElementById('sideB').value();
// let side3 = document.getElementById('sideC').value();
if (side1 === side2 && side1 === side3) {
console.log("triangle is Equilateral");
return 'Equilateral';
}
else if (side1 === side2 && side1 != side3) {
console.log("triangle is an isosceles");
return 'Isosceles';
}
else if (side1 != side2 && side1 != side3 && side2 != side3) {
console.log('triangle is a scalene');
return 'Scalene';
}
else if (side1 === null || side1 === undefined ||
side2 === null || side2 === undefined ||
side3 === null || side3 === undefined) {
console.log('You must enter a number for all 3 sides');
}
else {
console.log('You got me I don\'t know what this shape is. Maybe a rectangle?');
}
throw new Error('Not a triangle');
}
每个测试都应尝试函数的正确返回值,以便:
it("Should return equilateral when all sides are equal", function () {
expect(whichTriangle(1, 1, 1)).toBe('Equilateral');
});
it("Should return isosceles when 2 sides are equal", function () {
expect(whichTriangle(1, 2, 1)).toBe('Isosceles');
});
it("Should return scalene when no sides are equal", function () {
expect(whichTriangle(1, 2, 3)).toBe('Scalene');
});
但是,函数中仍然存在一个bug,请注意:
side1 === side2 && side3
这并不意味着三条边都相等。应该是:
side1 === side2 && side1 === side3 //or side2 === side3
这就是为什么你还要做其他的测试
it("Should not return equilateral when all sides are not equal", function () {
expect(whichTriangle(1, 1, 2)).not.toBe('Equilateral');
expect(whichTriangle(2, 1, 2)).not.toBe('Equilateral');
expect(whichTriangle(2, 3, 2)).not.toBe('Equilateral');
});
it("Should not return isosceles when 3 sides are equal or none are equal", function () {
expect(whichTriangle(2, 2, 2)).not.toBe('Isosceles');
expect(whichTriangle(3, 2, 1)).not.toBe('Isosceles');
});
it("Should not return scalene there's at least one side equal", function () {
expect(whichTriangle(1, 1, 2)).not.toBe('Scalene');
expect(whichTriangle(1, 2, 1)).not.toBe('Scalene');
expect(whichTriangle(2, 1, 1)).not.toBe('Scalene');
expect(whichTriangle(2, 2, 2)).not.toBe('Scalene');
});
它必须是toBe
而不是toBe
。我还注意到你试图用来说而不是代码>这不起作用。你需要做expect(某事)。not.toBe(某事)
看起来您已经知道前两个错误(文档选择器)的原因
这些就是代码抛出错误的原因。尽管您的单元测试和要测试的功能需要仔细重新考虑。您的单位描述函数的返回值应该是一个描述三角形的字符串。但是,您的函数不会返回字符串,而是打印到控制台。如果函数返回“等边”、“等腰”或“不等边”,则效果更好
function whichTriangle(sideA, sideB, sideC) {
// let side1 = document.getElementById('sideA').value();
// let side2 = document.getElementById('sideB').value();
// let side3 = document.getElementById('sideC').value();
if (side1 === side2 && side1 === side3) {
console.log("triangle is Equilateral");
return 'Equilateral';
}
else if (side1 === side2 && side1 != side3) {
console.log("triangle is an isosceles");
return 'Isosceles';
}
else if (side1 != side2 && side1 != side3 && side2 != side3) {
console.log('triangle is a scalene');
return 'Scalene';
}
else if (side1 === null || side1 === undefined ||
side2 === null || side2 === undefined ||
side3 === null || side3 === undefined) {
console.log('You must enter a number for all 3 sides');
}
else {
console.log('You got me I don\'t know what this shape is. Maybe a rectangle?');
}
throw new Error('Not a triangle');
}
每个测试都应尝试函数的正确返回值,以便:
it("Should return equilateral when all sides are equal", function () {
expect(whichTriangle(1, 1, 1)).toBe('Equilateral');
});
it("Should return isosceles when 2 sides are equal", function () {
expect(whichTriangle(1, 2, 1)).toBe('Isosceles');
});
it("Should return scalene when no sides are equal", function () {
expect(whichTriangle(1, 2, 3)).toBe('Scalene');
});
但是,函数中仍然存在一个bug,请注意:
side1 === side2 && side3
这并不意味着三条边都相等。应该是:
side1 === side2 && side1 === side3 //or side2 === side3
这就是为什么你还要做其他的测试
it("Should not return equilateral when all sides are not equal", function () {
expect(whichTriangle(1, 1, 2)).not.toBe('Equilateral');
expect(whichTriangle(2, 1, 2)).not.toBe('Equilateral');
expect(whichTriangle(2, 3, 2)).not.toBe('Equilateral');
});
it("Should not return isosceles when 3 sides are equal or none are equal", function () {
expect(whichTriangle(2, 2, 2)).not.toBe('Isosceles');
expect(whichTriangle(3, 2, 1)).not.toBe('Isosceles');
});
it("Should not return scalene there's at least one side equal", function () {
expect(whichTriangle(1, 1, 2)).not.toBe('Scalene');
expect(whichTriangle(1, 2, 1)).not.toBe('Scalene');
expect(whichTriangle(2, 1, 1)).not.toBe('Scalene');
expect(whichTriangle(2, 2, 2)).not.toBe('Scalene');
});
金融机构