2017-07-28 6 views
0

私は別の三角形の種類をテストするプログラムを書いています。これは私のテストのために持っているものです:Jasmine Tdd first try

(思考プロセスのために左のコメント)。

アプリ-spec.js

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 

私は間違ったことをしていることは分かっていますが、正しい方向への動きは非常に高く評価されており、批評は大歓迎です。

答えて

1

toBeではなく、tobeである必要があります。また、!を使用してnotと言っているように見えていることに気づきました。あなたがする必要があるexpect(something).not.toBe(somethingElse)

最初の2つのエラー(ドキュメントセレクタ)があった理由はすでにわかっているようです。

これは、コードがエラーをスローする理由です。ユニットテストとテストする機能は注意深く検討する必要があります。あなたのユニットは、関数の戻り値が三角形を記述する文字列でなければならないと記述しています。しかし、関数は文字列を返さず、コンソールに出力します。あなたの関数が 'Equilateral'、 'Isosceles'または 'Scalene'を返した方が良いでしょう。

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'); 
}); 

をしかし、あなたの関数内のバグがまだあります、それに気づく:

side1 === side2 && side3 

3面がすべて等しいとは限りません。それは次のようにする必要があります。

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'); 
}); 
+0

はありがとうございました。私は使用していた!その理由は私のIDE(webStorm)がそれを!私が_.not.toBe_と打ち込んだとき、それは私が知らなかったことかもしれないと思っていたかもしれません。私は変更を行い、試し続けます。ありがとうございました。 –

+0

@PerryCraft、私はいくつかの 'not'の例を追加しました – MinusFour

+0

これは優れていて、よく書かれています。おそらく私が今までに得た最高の解答です。ありがとう –

1

私が見る最初の問題は、その行である:

expect(whichTriangle(sideA, sideB, sideC).toEqual(sideA, sideB, sideC)); 

がすべきは、以下のようになります。

expect(whichTriangle(sideA, sideB, sideC)).toEqual(sideA, sideB, sideC); 

次の1、常にundefinedを返すwhichTriangle機能は、そう、あなたのテストは以下のようになります。 期待(未定義).toEqual(何か)。例えば

あなたがテストをしたい関数が適切な値を返す場合は、このようにこれを行うことができます:

const foo = function() { 
    return 10; 
} 

it('should return 10', function() { 
    expect(foo()).toEqual(10); 
}) 
+0

_expect_変更がそれを行い、続けていただきありがとうございます。私は一日前に尋ねたはずだったが、私はそれを私の脳に掘り起こそうと奮闘したかった。 –