我有问题让Chai的expect.to.throw在我的node.js应用程序的测试中工作。测试在抛出的错误上一直失败,但如果我在try和catch中包装测试用例并断言捕获的错误,它就可以工作。

是不是像我想的那样,期望。抛不掉?

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.

当前回答

另一种可能的实现比.bind()解决方案更麻烦,但它有助于说明expect()需要一个为覆盖的函数提供this上下文的函数,您可以使用call(),例如:

期望(函数(){model.get。调用(模型、“z”);}).to.throw(“…”);

其他回答

这个问题有很多很多重复,包括没有提到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

你可以使用任何断言库检查特定的错误:

Node

  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);
  }
});

乍一看,这可能令人困惑。就像骑自行车一样,一旦点击,它就会永远“点击”。

如果你已经在使用ES6/ES2015,那么你也可以使用箭头函数。它基本上与使用普通匿名函数相同,但更短。

expect(() => model.get('z')).to.throw('Property does not exist in model schema.');

我找到了一个很好的方法:

// The test, BDD style
it ("unsupported site", () => {
    The.function(myFunc)
    .with.arguments({url:"https://www.ebay.com/"})
    .should.throw(/unsupported/);
});


// The function that does the magic: (lang:TypeScript)
export const The = {
    'function': (func:Function) => ({
        'with': ({
            'arguments': function (...args:any) {
                return () => func(...args);
            }
        })
    })
};

它比我以前的版本更有可读性:

it ("unsupported site", () => {
    const args = {url:"https://www.ebay.com/"}; //Arrange
    function check_unsupported_site() { myFunc(args) } //Act
    check_unsupported_site.should.throw(/unsupported/) //Assert
});

来自doc…;)

因为你依赖于这个上下文:

当函数被.throw调用时丢失了什么 它没有办法知道这应该是什么

您必须使用以下选项之一:

将方法或函数调用包装到另一个函数中 绑定上下文 //将方法或函数调用包装到另一个函数中 Expect (function () {cat.meow();}) .to.throw ();//函数表达式 Expect (() => cat.meow()).to.throw();// ES6的箭头函数 //绑定上下文 期望(cat.meow.bind(猫).to.throw ();/ /绑定

另一种可能的实现比.bind()解决方案更麻烦,但它有助于说明expect()需要一个为覆盖的函数提供this上下文的函数,您可以使用call(),例如:

期望(函数(){model.get。调用(模型、“z”);}).to.throw(“…”);