如何期待与柴的流星错误

时间:2017-04-11 03:22:04

标签: unit-testing meteor chai

我想测试一下,如果在没有正确凭据的情况下调用,我的方法会抛出未经授权的错误。我怎么用柴?我看到柴的例子是

var err = new ReferenceError('This is a bad function.');
var fn = function () { throw err; }
expect(fn).to.throw(ReferenceError);
expect(fn).to.throw(Error);
expect(fn).to.throw(/bad function/);
expect(fn).to.not.throw('good function');
expect(fn).to.throw(ReferenceError, /bad function/);
expect(fn).to.throw(err);

所以我试过

let error = new Meteor.Error(UNAUTHORIZED, UNAUTHORIZED_REASON, 'detail');
chai.expect(Meteor.call('addItem', item)).to.throw(error);

但这不起作用。想法?

2 个答案:

答案 0 :(得分:2)

expect(fn).to.throw(Meteor.Error);

  it('Test Meteor Error', () => {
    expect(() => { throw new Meteor.Error('test');}).to.throw(Meteor.Error);
  });

答案 1 :(得分:2)

你可以这样做:

假设您有一个抛出以下错误的方法:

throw new Meteor.Error('unauthorised', 'You cannot do this.');

使用以下方法测试该错误:

it('will throw an error', function() {
    assert.throws(() => {

        //whatever you want to run that should throw the error goes here

    }, Meteor.Error, /unauthorised/); //change 'unauthorised' to whatever your error is
});