309

expect.to.thrownode.js アプリのテストでChai を動作させるのに問題があります。テストはスローされたエラーで失敗し続けますが、テスト ケースを 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();
});

失敗:

19 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.
4

7 に答える 7

205

この回答が言うように、次のように匿名関数でコードをラップすることもできます。

expect(function(){
    model.get('z');
}).to.throw('Property does not exist in model schema.');
于 2014-03-12T01:26:33.783 に答える
106

また、すでに ES6/ES2015 を使用している場合は、矢印関数も使用できます。基本的には通常の無名関数を使用するのと同じですが、より短くなります。

expect(() => model.get('z')).to.throw('Property does not exist in model schema.');
于 2016-04-08T13:20:05.433 に答える
90

この質問には、Chai アサーション ライブラリに言及していない質問を含め、非常に多くの重複があります。ここにまとめられた基本は次のとおりです。

アサーションは、すぐに評価するのではなく、関数を呼び出す必要があります。

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 work
function badReference() { x.y.z }; assert.throws(badReference);  
   // for the verbose
assert.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);
  }
});

これは、最初は混乱するかもしれません。自転車に乗るのと同じように、一度カチッとはまるだけです。

于 2017-04-16T18:01:19.363 に答える