javascript - 如何使用 practicalmeteor :mocha 对 meteor 方法进行单元测试

标签 javascript unit-testing testing meteor mocha.js

我正在为 meteor 编写单元测试做噩梦。有太多旧的、过时的文章和太少的清晰、相关的文档,我无法弄清楚我真正需要做什么才能让它发挥作用。

我遇到了一个又一个问题,只是真的希望有人能告诉我他们如何为我的一种方法编写测试,这样我就可以看到他们做了什么,并对我的其余方法进行逆向工程。

这是我想为其编写测试的方法:

Meteor.methods({
  'client.new':( clientDetails ) => {
    check( clientDetails, {
      name: String,
      numberTeamMembers: String
    });

    clientDetails.teamMembers = [];

    if(!Meteor.userId() || !Roles.userIsInRole(Meteor.userId(), 'administrator')) {
      throw new Meteor.Error('500', 'You are not authorised to do this.');
    }

    if(Clients.findOne({ name: clientDetails.name})) {
      throw new Meteor.Error('500', 'This client name already exists!');
    };

    return Clients.insert(clientDetails);
  },
});

到目前为止,我得到了以下内容:

import { Meteor } from 'meteor/meteor';
import { expect, be } from 'meteor/practicalmeteor:chai';
import { describe, it, before } from 'meteor/practicalmeteor:mocha';
import { resetDatabase } from 'meteor/xolvio:cleaner';
import { Random } from 'meteor/random';

import { Clients } from '/imports/api/clients/clients.js';

import '/imports/api/clients/server/methods.js';

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

  it('can create a Client', function(){
    let clientName = "Microsoft",
        numberTeamMembers = "5",
        data = {
          name: clientName,
          numberTeamMembers: numberTeamMembers
        };

    let userId = Accounts.createUser({username: "admin", email: "admin@admin.com", password: "password"});
    Meteor.users.update({ _id: userId }, { $set: { roles: [ 'administrator' ] }});

    let method = Meteor.server.method_handlers['client.new'];

    method.apply(userId, [data]);


    let client = Clients.findOne();

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

上面的测试抛出的错误首先是它告诉我 Meteor.userId 只能在方法调用中调用。在发布函数中使用 this.userId。 这是不相关的,因为这是我正在测试的方法。其次,该方法会抛出错误(“您无权执行此操作”),因此它无法识别 Meteor.userId() 或用户处于“管理员” Angular 色这一事实。

如果有人能告诉我他们将如何测试该方法,我将不胜感激!

谢谢

最佳答案

我认为你需要学习 Meteor.user() 方法,试试这个:

import { Meteor } from 'meteor/meteor';
import { resetDatabase } from 'meteor/xolvio:cleaner';
import { sinon } from 'meteor/practicalmeteor:sinon';

describe('...', () => {
  let currentUser;

  beforeEach(() => {
    resetDatabase();
    Factory.define('user', Meteor.users, {
      profile: {
        // ...
      },
    });
    currentUser = Factory.create('user');
    sinon.stub(Meteor, 'user');
    Meteor.user.returns(currentUser);
  });

  afterEach(() => {
    Meteor.user.restore();
    resetDatabase();
  });

  it('...', () => {
    // ...
  });
});

您需要添加这些包:dburles:factoryxolvio:cleanerpracticalmeteor:sinon

关于javascript - 如何使用 practicalmeteor :mocha 对 meteor 方法进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41316635/

相关文章:

python - 为什么 caplog.text 是空的,即使我正在测试的函数正在记录?

c - 在单元测试过程代码时删除依赖项

django - 测试 Django Rest Framework 时如何为自定义用户反转路径

javascript - 为什么用 Node.js 运行 Mocha 第一次可以,但第二次不行?

javascript - 单页结账未继续下一步

Javascript 作用域 addEventListener 和 this

c# - 在单元测试中出现错误 "Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl not found"但在主程序中却没有

python - 使用 Unittest Python 进行测试

javascript - 动态添加选项卡 upgradeElement

javascript - 在javascript中从json中获取值