我在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);
}
它工作正常,如果我刪除異常代碼。任何建議或想法在這有什麼不對?
你正在使用什麼版本的茉莉花?在最近的Jasmine版本中,'toThrow'不需要參數。改用['toThrowError'](http://jasmine.github.io/2.0/introduction.html#section-Included_Matchers)。 –
這是茉莉-1.3.1。其他例外與** toThrow **正常工作。 – Hearty
在這種情況下,'doAddition(1,2)'根本不會拋出......您的測試存在缺陷。 –