javascript - 测试时将 Meteor.userId 传递给经过验证的方法

标签 javascript unit-testing meteor mocha.js meteor-methods

我有一个经过验证的方法,正在为其编写测试。该方法检查用户是否是管理员,如果不是则抛出错误。

我正在使用 dburles:factory 在 Meteor.users 集合中创建一个具有“管理员” Angular 色的新用户。

然后,我使用“admin”用户的 userId 调用经过验证的方法,但它引发了错误。

尽管我根据文档使用管理员用户的上下文调用该方法,但它似乎并未将其传递给该方法。当我在方法中 console.log(this.userId); 时,它返回未定义。

任何人都可以检查我的代码并告诉我为什么会发生这种情况吗?谢谢!

方法代码:

import { Meteor } from 'meteor/meteor';
import { Clients } from '../../clients';
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { Roles } from 'meteor/alanning:roles';

export const createClient = new ValidatedMethod({
    name: 'Clients.methods.create',
    validate: new SimpleSchema({
        name: { type: String },
        description: { type: String },
    }).validator(),
    run(client) {

        console.log(this.userId); //this is undefined for some reason

        if(!Roles.userIsInRole(this.userId, 'administrator')) {
            throw new Meteor.Error('unauthorised', 'You cannot do this.');
        }
        Clients.insert(client);
    },
});

测试代码:

import { Meteor } from 'meteor/meteor';
import { expect, be } from 'meteor/practicalmeteor:chai';
import { describe, it, before, after } from 'meteor/practicalmeteor:mocha';
import { resetDatabase } from 'meteor/xolvio:cleaner';
import { sinon } from 'meteor/practicalmeteor:sinon';
import { Factory } from 'meteor/dburles:factory';

import { createClient } from './create-client';
import { Clients } from '/imports/api/clients/clients';

describe('Client API Methods', function() {
  afterEach(function() {
    resetDatabase();
  });

  it('Admin user can create a new client', function() {
    let clientName = "Test",
        description = "This is a description of the client!",
        data = {
          name: clientName,
          description: description
        };

    Factory.define('adminUser', Meteor.users, {
      email: 'admin@admin.com',
      profile: { name: 'admin' },
      roles: [ 'administrator' ]
    });

    const admin = Factory.create('adminUser');

    console.log(Roles.userIsInRole(admin._id, 'administrator'));// this returns true

    //invoking the validated method with the context of the admin user as per the documentation
    createClient._execute(admin._id, data);

    let client = Clients.findOne();


    expect(Clients.find().count()).to.equal(1);
    expect(client.name).to.equal(clientName);
    expect(client.description).to.equal(description);
  });

最佳答案

我已经找到了解决问题的方法。

当您执行经过验证的方法时,您需要将 userId 作为对象传递,例如 { userId: j8H12k9l98UjL }

我将其作为字符串传递,因此不会使用 Factory 创建的用户的上下文来调用该方法。

此测试现在完美运行

希望这对其他人有帮助

关于javascript - 测试时将 Meteor.userId 传递给经过验证的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41538404/

相关文章:

javascript - 如何推送FileList中的文件?

javascript - 使用新的 facebook 图形 api,ajax 调用返回 null(空)

unit-testing - 在 PHPUnit/DBUnit 中关闭外键约束

javascript - 如何覆盖 JS 库中的变量

javascript - 来 self 的 app.meteor.com 的 Meteor.http.call()

css - 如何在 Meteor 中只加载一页的 css 文件?

javascript - IE7 和 8 将不需要的阴影添加到另一个表格内的表格

javascript - 如何定位元素的 'parent'

java - 使用内部类对类进行单元测试的正确方法

javascript - 移动通信大会 : how to rerender on URL change