0
我正在用sinon.JS編寫簡單的單元測試。被測代碼有某種類型的檢查,這將引發類型錯誤:Sinon的spy.threw()在傳遞錯誤參數時的行爲不符合預期?
Tx = class Tx {
constructor(date) {
if (!(date instanceof Date)) throw new TypeError();
this.date = date;
}
}
在我test.js,我使用興農測試的Tx實例化時失敗的說法是不是一個日期:
describe('Tx', function() {
describe('#constructor', function() {
it('should fail when not passed a date as 1st param', function() {
var txSpy = sinon.spy(Tx.constructor);
try {
tx = new Tx(true);
} catch (e) {
// test success
console.log('Error: '+e)
}
}
}
}
result = txSpy.threw(new TypeError());
assert(result);
斷言失敗,即使catch輸出爲「Error:TypeError」;在傳遞錯誤類型的字符串描述(即「TypeError」,根據the sinon API)時會出現相同的結果。
任何提示?在此先感謝:)
我認爲這可能是這種情況......但那麼我將如何檢查類型相等而不是實例相等? – Navonod
@Navonod我用最好的方式編輯過,但你應該知道我從來沒有用過'sinon'。雖然我認爲我的答案應該能解決你眼前的問題,但在某些情況下可能會失敗,或者根本就不是最佳做法。 – Aaron