2012-08-24 1 views
1

이 코드를예외를 테스트하기 위해 CoffeeScript에서 Jasmine을 얻으려면 어떻게해야합니까?</p> <pre><code>class root.Goal constructor: (@name, @size) -> if @size <= 0 then throw new Error "Goal must be larger than 0 size" </code></pre> <p>이 재스민 - 커피 스크립트 테스트에서 테스트 :

예외가 발생하지만, 시험에 의해 처리되지 가져옵니다 나타납니다
it "cannot be of size 0", -> 
    expect(new p.Goal("Goal 3", 0)).toThrow "Goal must be larger than 0 size" 

:

cannot be of size 0 
Failures: 
    1) cannot be of size 0 
    Message: 
    Error: Goal must be larger than 0 size 
    Stacktrace: 
    Error: Goal must be larger than 0 size 
    at new Goal (/var/lib/stickshift/1d4f33cd01e442eaa154aed2e7697ca7/app-root/data/235917/prioritization/process.coffee:14:15) 

아이디어가 있으십니까?

+1

가능한 [오류가 발생할 것으로 예상되는 테스트를 작성하는 방법] (http://stackoverflow.com/questions/4144686/how-to-write-a-test-which-expects-an-error) -to-be-throw) –

+0

어쩌면 그렇게 생각 했으므로 익명 함수로 "p.Goal ("Goal 3 ", 0)을 호출하려고했습니다. expect (f = do -> new p.Goal ("Goal 3", 0)). 그러나 그 중 하나가 작동하지 않았습니다. –

+2

이 구문이 효과가 있음이 밝혀졌습니다. expect (-> new p.Goal ("Goal 3", 0)). 0보다 커야합니다 " –

답변

1

이미 익숙한 기능으로 오류 발생 코드에 대한 호출을 래핑해야합니다. 예상 호출을하지 않으면 오류가 발생하는 코드의 "결과"(있는 경우)가 표시됩니다. 따라서 기대가 불려지면 모든 "행동"은 이미 끝났습니다.

관련 문제