我試圖實現在JavaScript中學習測試驅動開發方面打印鑽石的功能。茉莉花拋出錯誤期望().Throw而不是識別拋出的錯誤
Diamond.prototype.outerSpace = function (current, widest) {
var currentValue = this.getIndexOf(current);
var widestValue = this.getIndexOf(widest);
if (currentValue > widestValue) {
throw new Error('Invalid combination of arguments');
}
var spaces = widestValue - currentValue;
return new Array(spaces + 1).join(' ');
};
我在錯誤處理中遇到問題。如果currentValue大於widestValue,上面的函數應該會拋出一個錯誤。
這是代表在測試/規格我的片斷:
it ("should throw an exception, if it is called with D and C", function() {
var outerSpace = diamond.outerSpace.bind(diamond, 'D', 'C');
expect(outerSpace).toThrow('Invalid combination of arguments');
});
我也試圖與一個匿名函數在期待(..),但是這也沒有工作。
控制檯消息是:預期的函數拋出'Inval ...'但它拋出錯誤:無效的參數組合。
我不明白,我該怎麼處理這些信息。
編輯:這很奇怪,因爲它與Jasmine v.1.3一起工作,但它並不適用於茉莉花v.2.3,即與業力,儘管基於茉莉花的代碼。