第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

暮色呼如 2019-07-05 13:37:38
摩卡/柴期托.拋出不捕捉拋出的錯誤我有問題去拿柴氏的expect.to.throw在我的node.js應(yīng)用程序的測試中工作。測試在拋出的錯誤上一直失敗,但是如果我將測試用例包裝在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條經(jīng)驗 獲得超3個贊

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

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


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

TA貢獻1719條經(jīng)驗 獲得超6個贊

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

斷言必須調(diào)用函數(shù),而不是立即計算。

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

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

節(jié)點

  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./)

應(yīng)

  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./);

您必須處理“轉(zhuǎn)義”測試的異常。

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

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


查看完整回答
反對 回復(fù) 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 600 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號