3

I found it a little complicated, and more complicated if I wrote my meteor methods in /lib folder, that I want is to test from server test folder my methods (unit test), but stub this.userId and also debugging or showing logs in server side does not help too much.

I was having too much problems with it, I'm using mochajs with velocity, does anyone would help me please? is someone know how can I write the units to meteor methods?

Ethaan
  • 11,291
  • 5
  • 35
  • 45
Zilev av
  • 491
  • 1
  • 7
  • 21
  • 1
    See the discussion [here](https://forums.meteor.com/t/testing-methods-which-use-this-userid/). It may be that `callInContext` is what you need, but I'm not sure. – David Weldon Apr 02 '15 at 21:54
  • thank you so much David, there is too much helpful information in the link you provide me – Zilev av Apr 22 '15 at 22:23

1 Answers1

2

Mocha doesn't support unit tests, only Jasmine does currently. This is an example of how you would write a unit test in Jasmine for the server and use userId.

  it("should return premium content to logged in users", function () {

// SETUP
var thisContext = {
  userId : true
};

var expectedCursor = 'chapter_cursor1';
var _query = true, _modifiers = true;
Chapters.find = function(query, modifiers) {
  _query = query;
  _modifiers = modifiers;
  return expectedCursor;
};

// EXECUTE
var actualCursor = Meteor.publishFunctions['chapters'].apply(thisContext);

// VERIFY
expect(actualCursor).toBe(expectedCursor);
expect(_query).toBe(undefined);
expect(_modifiers).toBe(undefined);

});

Taken from here: https://github.com/xolvio/Letterpress/blob/master/tests/jasmine/server/unit/chaptersSpec.js#L3

Xolv.io
  • 2,483
  • 1
  • 15
  • 17
  • The Meteor Test Manual, this Meteor.publishFunctions['chapters'].apply(thisContext); applies only to publish functions???? and for meteor methods??? – Zilev av Apr 06 '15 at 18:41
  • here a good example, Meteor.methodMap.serverMethod.call(thisContext), http://stackoverflow.com/questions/28796568/meteor-jasmine-velocity-how-to-test-a-server-method-requiring-logged-in-us – Zilev av Apr 07 '15 at 20:35
  • the link is dead, and Meteor.publishFunctions is undefined on my meteor 1.2.1 server side – Alexander Skiller Jul 26 '16 at 15:35