Use Meteor.call() with jasmine doesn't work


#1

Hello,

This is the file that I got here tests/jasmine/integration/test.js
In the variable data I have many things but I didn’t put anything here to not pollute the ticket.

describe('Games', function() {
    beforeEach(function() {
        Games.remove({});
    });

    it('Create a game', function() {
        var data = {};
        var nbGames = 0;
        Meteor.call('gameInsert', data, function(error, result) {
            if (error) {
                console.log(error.message);
            }
        });
        nbGames = Games.find().count();
        expect(nbGames).toEqual(1);
    });
});

This code doesn’t pass but this one below does :

describe('Games', function() {
    beforeEach(function() {
        Games.remove({});
    });

    it('Create a game', function() {
        var data = {};
        var nbGames = 0;
        Games.insert(data);
        nbGames = Games.find().count();
        expect(nbGames).toEqual(1);
    });
}); 

The Meteor.call() doesn’t add a document in the DB, is there a way to test methods ???

Thanks in advance for the help.


#2

Is it simply because you are using a callback, but ignoring the results of that callback (i.e. nothing to do with Jasmine)?

From the documentation:

If you include a callback function as the last argument (which can’t be an argument to the method, since functions aren’t serializable), the method will run asynchronously: it will return nothing in particular and will not throw an exception. When the method is complete (which may or may not happen before Meteor.call returns), the callback will be called with two arguments: error and result. If an error was thrown, then error will be the exception object. Otherwise, error will be undefined and the return value (possibly undefined) will be in result.

Maybe try:

describe('Games', function() {
    beforeEach(function() {
        Games.remove({});
    });

    it('Create a game', function() {
        var data = {};
        var nbGames = 0;
        Meteor.call('gameInsert', data, function(error, result) {
            if (error) {
                console.log(error.message);
            } else {
                nbGames = Games.find().count();
                expect(nbGames).toEqual(1);
            }
        });
    });
});

#3

Thanks for your help, it works now !