Meteor login method test

Hi everyone,
I’m writing unit test for my Meteor 1.4.2 application, where few of my methods requires a user need to be logged in before processing.
How should I test my methods?

So far, I’ve written a test case to create a new user and login with that user

  describe('login method', function () {
    let logingKey;
    beforeEach(function () {
      const createUser = Meteor.server.method_handlers['registerUser'];
      let params = {
        username: 'testUsername'
      res = createUser.apply({}, [params]);
      logingKey = res.key;

    it('can provide authentication', function () {
      const loginUser = Meteor.server.method_handlers['login'];
      let params = {
        key: logingKey
      loginUser.apply({}, [params]);

where I’ve written a custom login handler to login with the generated key which works fine with application, but in test results I’m getting following error.

Error: Cannot read property 'id' of undefined
    at AccountsServer.Ap._setLoginToken (packages/accounts-base/accounts_server.js:889:35)
    at packages/accounts-base/accounts_server.js:288:10
    at Object.Meteor._noYieldsAllowed (packages/meteor.js:671:12)
    at AccountsServer.Ap._loginUser (packages/accounts-base/accounts_server.js:287:10)
    at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:349:12)
    at Object.methods.login (packages/accounts-base/accounts_server.js:533:21)
    at Object.methodMap.(anonymous function) (packages/meteorhacks_kadira.js:2731:30)
    at Test.<anonymous> (imports/api/methods/loginUser.tests.js:30:17)
    at run (packages/practicalmeteor:mocha-core/server.js:34:29)
    at Context.wrappedFunction (packages/practicalmeteor:mocha-core/server.js:63:33)

What could be wrong here? any suggestions are welcome.

1 Like