jeudi 15 septembre 2016

Meteor method callback not firing in Mocha full-app test

I am testing my Meteor (v. 1.4) app in Meteor full-app testing mode using Mocha. In the test I create a new user. I have an after hook remove the user by calling a Meteor method.

after(function(done) {
  var testUser = Meteor.users.findOne({'profile.fullname':'test test'});
  if (testUser) {
    Meteor.call('test.removeUser', {_id: testUser._id}, function(err, docId) {
      done(err);
    });
  }
  else done();
});

Here is my Meteor method code:

Meteor.methods({
 'test.removeUser': function(selector) {
   var result = Meteor.users.remove(selector);
   return result;
 },

In the Mocha reporter, the after hook times out. It doesn't seem to hit the done() callback. Using the debugger in the console indicates that it ignores the Meteor call's callback function completely.

I have used Meteor methods successfully in this way in other parts of my app. E.g. In the client:

            Meteor.call('insertData', insertDoc, function(error, docId){
              if(error) {
                displayError('Failed to create the data set. Please try again.', error);
              }
              else {
                Router.go('/data/display/' + docId);
              }
            });

In the Meteor methods in the server:

'insertData': function(dataDoc) {
    var userId = this.userId;
    if( !Roles.userIsInRole(userId, 'edit', 'data') ) {
        throw new Meteor.Error('not-authorized')
    }
    else {
      var dId = Data.insert(dataDoc);
      var group = 'data ' + dId;
      Roles.addUsersToRoles(userId, 'edit', group);
      console.log('Data inserted: ' + dataDoc.name);
      return dId;
    }
},

Why is it not working here?

Aucun commentaire:

Enregistrer un commentaire