2016-04-06 49 views
0

我在Jasmine中爲算術運算創建了一個簡單的測試用例,其中給出了錯誤預期的函數拋出異常。雖然我已經這樣做了。Jasmine javaScript異常

規範文件

describe("Arithmetic Operation",function(){ 
    it("adds 2 numbers i.e. 5,5",function(){ 
     expect(doAddition(5,5)).toEqual(10); 
    }); 

    it("throws an error while adding", function() { 
     expect(function() {doAddition(1,2)}).toThrow(new Error("Not allowed.")); 
    }); 
}); 

的src文件

function doAddition(a,b){ 
    return parseInt(a) + parseInt(b); 
} 

它工作正常,如果我刪除異常代碼。任何建議或想法在這有什麼不對?

+0

你正在使用什麼版本的茉莉花?在最近的Jasmine版本中,'toThrow'不需要參數。改用['toThrowError'](http://jasmine.github.io/2.0/introduction.html#section-Included_Matchers)。 –

+0

這是茉莉-1.3.1。其他例外與** toThrow **正常工作。 – Hearty

+0

在這種情況下,'doAddition(1,2)'根本不會拋出......您的測試存在缺陷。 –

回答

2

你期望doAddition(1,2)扔,它不。因此測試失敗。

+0

在** 5,5 **的情況下,它也拋出相同的錯誤,並且是來自茉莉花**新的jasmine.ExpectationResult **異常,而不是自定義錯誤消息。 – Hearty

+1

'parseInt(5)+ parseInt(5)'**不會拋出**。它返回'10' ...你的第一個測試驗證了這一點。 –