如何用实际流量单元测试流星方法:mocha [英] How to unit test a meteor method with practicalmeteor:mocha

查看:79
本文介绍了如何用实际流量单元测试流星方法:mocha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为流星编写单元测试单元测试。有太多旧的,过时的文章和太少的清晰,相关的文档,以便我能够找出实际需要做的事情来实现这一点。

I'm having a nightmare writing unit tests for meteor. There are too many old, outdated articles and too few clear, relevant pieces of documentation for me to be able to work out what I actually need to do to get this to work.

我遇到问题之后遇到问题而且真的希望有人能告诉我他们如何为我的一种方法编写测试,这样我就可以看到他们有什么完成并对其余方法进行反向工程。

I'm running in to problem after problem and just really hope someone can show me how they would write a test for one of my methods so I can see what they have done and reverse engineer it for the rest of my methods.

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

Here's a method I'd like to write a test for:

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);
  },
});

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

So far I've got the below:

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()或用户处于管理员角色的事实。

The errors the above test throws are firstly it tells me that Meteor.userId can only be invoked in method calls. Use this.userId in publish functions. which is irrelevant because this is a method I'm testing. Secondly, the method throws the error ('You are not authorised to do this') so either it doesn't recognise the Meteor.userId() or the fact that the user is in the 'administrator' role.

如果有人能告诉我他们将如何测试这种方法我真的很感激!

If someone could show me how they would test that method I'd really appreciate it!

谢谢

推荐答案

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

I think you need to stud the Meteor.user() method, try this:

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:factory xolvio:cleaner practicalmeteor:sinon

You need to add these packages: dburles:factory, xolvio:cleaner, practicalmeteor:sinon

这篇关于如何用实际流量单元测试流星方法:mocha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆