【发布时间】:2017-09-28 23:11:07
【问题描述】:
我正在使用经过验证的方法 (mdg:validated-method) 和 LoggedInMixin (tunifight:loggedin-mixin)。
现在我的单元测试有问题,因为它们因notLogged 错误而失败,因为在单元测试中当然没有登录用户。
我该如何存根?
方法
const resetEdit = new ValidatedMethod({
name: 'reset',
mixins: [LoggedInMixin],
checkLoggedInError: { error: 'notLogged' }, // <- throws error if user is not logged in
validate: null,
run ({ id }) {
// ...
}
})
单元测试
describe('resetEdit', () => {
it('should reset data', (done) => {
resetEdit.call({ id: 'IDString' })
})
})
单元测试抛出Error: [notLogged]。
【问题讨论】:
-
你试过模拟 Meteor.user 和 Meteor.userId 吗?您还可以尝试在运行测试之前与用户一起创建夹具,然后在运行测试之前使用该用户登录
-
我不太确定该怎么做,因为我正在使用
meteor test --once命令在我的 CI 工作流程中进行单元测试。所以我想我无法登录任何用户... -
你可以,在你的测试中你应该能够做类似
Meteor.loginWithPassword(user, password, function({ resetEdit.call({id: "IDString"}); })); -
你试过
resetEdit.call.call({ userId: Random.id() }, { id: 'IDString' })吗?
标签: javascript unit-testing meteor stub