2017-07-20 33 views
0

我想为我的流星应用做一些单元测试(mocha/chai)。我正在使用已验证的方法(这对此应该不重要)。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 } 
    }) 
    } 
}) 

回答

0

在测试模式下,你可以使用_execute执行有效的方法来传递上下文,看here。然而,最简单的事情在这里似乎存根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 }) 
    } 
}) 
+0

我得到的错误'错误:试图包裹userIsInRole这已经是wrapped' – user3142695

+0

好像你正试图把它包起来两次.. 。 – tomsp