5
我正在为流星写单元测试的噩梦。有太多陈旧过时的文章和太少的清晰相关的文档让我能够弄清楚为了实现这个目标我需要做些什么。如何用流体单元测试流星法:摩卡
我遇到了问题后的问题,只是真的希望有人能告诉我他们将如何为我的一种方法编写测试,以便我可以看到他们已经完成了什么,并对其余的方法进行反向工程。
这是我想编写一个测试方法:
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: "[email protected]", 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 can only be invoked in method calls. Use this.userId in publish functions.
这是不相关的,因为这是我测试的一种方法。其次,该方法会抛出错误('您无权执行此操作'),因此无法识别Meteor.userId()或用户处于'管理员'角色的事实。
如果有人能告诉我他们将如何测试该方法,我会非常感激!
感谢
用户需要先登录在调用方法之前。即使我也遇到了同样的问题。让我知道你是否发现任何东西。 –