如何用 chai 预测流星错误
How to expect a meteor error with chai
我想测试如果在没有正确凭据的情况下调用我的方法会引发未经授权的错误。我将如何使用 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);
但这没有用。想法?
是expect(fn).to.throw(Meteor.Error);
it('Test Meteor Error', () => {
expect(() => { throw new Meteor.Error('test');}).to.throw(Meteor.Error);
});
你可以这样做:
假设您有一个抛出以下错误的方法:
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
});
我想测试如果在没有正确凭据的情况下调用我的方法会引发未经授权的错误。我将如何使用 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);
但这没有用。想法?
是expect(fn).to.throw(Meteor.Error);
it('Test Meteor Error', () => {
expect(() => { throw new Meteor.Error('test');}).to.throw(Meteor.Error);
});
你可以这样做:
假设您有一个抛出以下错误的方法:
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
});