Unable to call Meteor methods from mocha

Im working on a project built in Meteor + angular1 .
We are using Mocha for writing unit test-cases
.Iam unable to call a serverside method from my testjs file.I have my test file in imports folder.

my server-side method:
Meteor.methods({
testcall:function() {
console.log(‘hello there’);
return “luck”;
}

});
my test-case:

describe(‘testcall’, function () {
it(‘shuold call testcall method’, function () {
Meteor.call(‘testcall’, function(error, result) {
console.log(error || result);
});
});
});

ERROR:
"Method testcall not found’’.

Any ideas?

Thanks

I’m trying to do the same thing.

I got closer with

const result = Meteor.methods.call('testMethod', arguments)
assert.equal(result, 'ok', 'method worked')

But had an error saying arguments must be a function ?

Hi,
please can you follow the code ,let me know

const result = Meteor.methods.call(‘testMethod’, function() { return [argument1, angument2] });

Hi Bmordan,
Iam facing the same problem .
Did u get the solution?

Thanks in advance.

how to write a test case for ‘’‘getuserdata’’. in mocha

describe(“test”,function(){
it(“testing”,function(){
Meteor.call(“getUserPersonalData”,(error,result) => {
if(error){
console.log(error);
}
else {
console.log(result);
}
});
});
});

any help?

thanks.

There is another way, which is to run your Meteor method tests on the server, not on the client.

Take a look at https://guide.meteor.com/methods.html, and find the section Advanced Method Boilerplate, then look for the paragraph Defining.

You rewrite your Meteor methods so that you can call the parts that do the work like normal functions.
It works very well.

There is a gotcha, which is that you must remember that everything on the server runs synchronously.
I started trying to call my new functions - from the server - using callbacks, and could not understand why they were not working. You don’t need or want callbacks in your tests on the server, because on the server you don’t have to wait for the reply.

1 Like