我想測試一下,我的構造函數會使用Teaspoon gem for Rails和ChaiJS作爲我的斷言庫來拋出一個錯誤。ChaiJS期望構造函數拋出錯
當我運行下面的測試:
it('does not create the seat if x < 0', function() {
var badConstructor = function() {
return new Seat({ radius: 10, x: -0.1, y: 0.2, seat_number: 20, table_number: 30});
};
expect(badConstructor).to.throw(Error, 'Invalid location');
});
我得到這樣的輸出:
失敗:
1) Seat does not create the seat if x < 0
Failure/Error: undefined is not a constructor (evaluating 'expect(badConstructor).to.throw(Error(), 'Invalid location')')
構造函數拋出的錯誤,但我想我不會寫測試正確。
當我嘗試運行expect(badConstructor())
然後我得到的輸出:
Failures:
1) Seat does not create the seat if x < 0
Failure/Error: Invalid location
它是否意味着'expect(badConstructor())'?即,您需要調用該函數。 – Andy
我剛試過 - 無濟於事。現在我得到另一個錯誤輸出 - 我將它添加到OP中。 – ThomYorkkke
所以它聽起來像是正常工作,因爲「無效位置」是錯誤信息的一部分。您需要在測試中選擇它,或者在函數中添加一個try/catch語句來返回可以測試的錯誤。 – Andy