Search code examples
unit-testingmeteormocha.jsserver-sidemeteor-velocity

how can I write unit test to my meteor methods?


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?


Solution

  • 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