How to unit test a meteor method with a practical meteorite: mocha

I have nightmare block tests for a meteor. Too many old, outdated articles and too few clear, relevant documents for me to be able to develop what I really need to do to get this to work.

I ran into a problem after the problem and just hope someone can show me how they will write a test for one of my methods so that I can see what they did and reverse engineer it for the rest of my methods.

Here is the method I would like to write for the test:

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 have received the following:

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

The errors of the above test throws, firstly, tell me that Meteor.userId can only be invoked in method calls. Use this.userId in publish functions. Meteor.userId can only be invoked in method calls. Use this.userId in publish functions. that doesn’t matter because it is the method that I am testing. Secondly, the method throws an error ("You are not authorized to do this"), so that either it does not recognize Meteor.userId () or the fact that the user is in the "administrator" role.

If someone can show me how they will test this method, I would really appreciate it!

thanks

+5
source share
1 answer

It seems to me that you need to fix the Meteor.user() method, try the following:

 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('...', () => { // ... }); }); 

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

+1
source

Source: https://habr.com/ru/post/1261851/


All Articles