MeteorJS:用户及其角色的虚拟人进行单元测试



我想为我的流星应用程序做一些单元测试(摩卡/柴(。我正在使用经过验证的方法(这无关紧要(。

在我的方法中,我正在检查用户是否具有执行集合更新的管理员权限。

如何在单元测试中设置"虚拟",因为现在测试将始终失败并引发 403 错误。

单元测试

describe('method', () => {
it('should update document', (done) => {
articleUpdate.call({ _id, value })
}
})

方法

const articleUpdate = new ValidatedMethod({
name: 'article.update',
validate: null,
run ({ _id, value }) {
const loggedInUser = Meteor.user()
const isAdmin = Roles.userIsInRole(loggedInUser, ['group'], 'admin')
if (!isAdmin) { throw new Meteor.Error(403, 'Access denied') }
Articles.update(_id, {
$set: { content: value }
})
}
})

在测试模式下,您可以使用_execute执行经过验证的方法以传递上下文,请参阅此处。但是,这里最简单的事情似乎是像这样存根Roles.userIsInRole

import { sandbox } from 'sinon';
const sb = sandbox.create();
describe('method', () => {
it('should update document', () => {
sb.stub(Roles, 'userIsInRole').callsFake(() => true);
articleUpdate.call({ _id, value })
}
})

最新更新