2017-07-28 63 views
0

我正在写一个程序来测试不同的三角形类型,我卡住了。这是我对我的测试:茉莉花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(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); 

这是我收到的时候我运行应用程序:

Jasmine Spec Runner

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 

并不意味着所有的三面都是平等的。它应该是:

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

下一个,功能whichTriangle总是返回未定义的,所以你的测试看起来像这样:期望(未定义).toEqual(某物)。

例如,当你想测试,如果函数返回正确的值,你可以做到这一点是这样的:

const foo = function() { 
    return 10; 
} 

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

感谢您的期望改变将会继续发挥作用。我应该在一天前问过,但我想努力把它挖掘到我的大脑中。 –