亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

摩卡/柴期托.拋出不捕捉拋出的錯誤

摩卡/柴期托.拋出不捕捉拋出的錯誤

暮色呼如 2019-07-05 13:37:38
摩卡/柴期托.拋出不捕捉拋出的錯誤我有問題去拿柴氏的expect.to.throw在我的node.js應用程序的測試中工作。測試在拋出的錯誤上一直失敗,但是如果我將測試用例包裝在TRY和CATCH中,并斷言捕獲的錯誤,它就能工作。是嗎?expect.to.throw不是像我想的那樣工作還是怎么的?it('should throw an error if you try to get an undefined property', function (done) {   var params = { a: 'test', b: 'test', c: 'test' };   var model = new TestModel(MOCK_REQUEST, params);   // neither of these work   expect(model.get('z')).to.throw('Property does not exist in model schema.');   expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.'));   // this works   try {      model.get('z');    }   catch(err) {     expect(err).to.eql(new Error('Property does not exist in model schema.'));   }   done();});失?。?9 passing (25ms)   1 failing   1) Model Base should throw an error if you try to get an undefined property:      Error: Property does not exist in model schema.
查看完整描述

3 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

這個答案說,您也可以將代碼包裝在一個匿名函數中,如下所示:

expect(function(){
    model.get('z');}).to.throw('Property does not exist in model schema.');


查看完整回答
反對 回復 2019-07-05
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

這個問題有很多重復的問題,包括沒有提到柴斷言庫的問題。以下是匯集在一起的基本知識:

斷言必須調用函數,而不是立即計算。

assert.throws(x.y.z);      
   // FAIL.  x.y.z throws an exception, which immediately exits the
   // enclosing block, so assert.throw() not called.assert.throws(()=>x.y.z);  
   // assert.throw() is called with a function, which only throws
   // when assert.throw executes the function.assert.throws(function () { x.y.z });   
   // if you cannot use ES6 at workfunction badReference() { x.y.z }; assert.throws(badReference);  
   // for the verboseassert.throws(()=>model.get(z));  
   // the specific example given.homegrownAssertThrows(model.get, z);
   //  a style common in Python, but not in JavaScript

可以使用任何斷言庫檢查特定錯誤:

節點

  assert.throws(() => x.y.z);
  assert.throws(() => x.y.z, ReferenceError);
  assert.throws(() => x.y.z, ReferenceError, /is not defined/);
  assert.throws(() => x.y.z, /is not defined/);
  assert.doesNotThrow(() => 42);
  assert.throws(() => x.y.z, Error);
  assert.throws(() => model.get.z, /Property does not exist in model schema./)

  should.throws(() => x.y.z);
  should.throws(() => x.y.z, ReferenceError);
  should.throws(() => x.y.z, ReferenceError, /is not defined/);
  should.throws(() => x.y.z, /is not defined/);
  should.doesNotThrow(() => 42);
  should.throws(() => x.y.z, Error);
  should.throws(() => model.get.z, /Property does not exist in model schema./)

柴期待

  expect(() => x.y.z).to.throw();
  expect(() => x.y.z).to.throw(ReferenceError);
  expect(() => x.y.z).to.throw(ReferenceError, /is not defined/);
  expect(() => x.y.z).to.throw(/is not defined/);
  expect(() => 42).not.to.throw();
  expect(() => x.y.z).to.throw(Error);
  expect(() => model.get.z).to.throw(/Property does not exist in model schema./);

您必須處理“轉義”測試的異常。

it('should handle escaped errors', function () {
  try {
    expect(() => x.y.z).not.to.throw(RangeError);
  } catch (err) {
    expect(err).to.be.a(ReferenceError);
  }});

一開始這看起來很混亂。就像騎自行車一樣,一旦它發出咔嚓聲,它就會永遠“咔嚓”作響。


查看完整回答
反對 回復 2019-07-05
  • 3 回答
  • 0 關注
  • 604 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號