Hayk Safaryan
Hayk Safaryan

Reputation: 2046

Error: Match error: Expected object, got string in the test

Hi guys I am trying to write a test for my meteor method. So I created a file accountsMethods.js in the server folder

import { Meteor } from 'meteor/meteor'

Meteor.methods({
  'createUser': function (email, password) {
    var userObject = {
      email,
      password
    }

    Accounts.createUser(userObject)
  }
})

And accountsMethods.tests.js

import { Meteor } from 'meteor/meteor'
import { resetDatabase } from 'meteor/xolvio:cleaner'
import { Random } from 'meteor/random'
import should from 'should'

describe('accountsMethods', function () {
  beforeEach(function () {
    resetDatabase()
  })

  it('Creates User', function () {
    const createUser = Meteor.server.method_handlers['createUser']
    const email = '[email protected]'
    const password = '12345'
    const userId = Random.id()
    createUser.apply({ userId }, [email, password])
    should(Meteor.users.find({}).count()).be.exactly(1)
  })
})

In the tests I have the following error but I am not sure what this is about.

Error: Match error: Expected object, got string
    at exports.check (packages/check.js:57:15)
    at packages/accounts-password/password_server.js:1033:7
    at tryLoginMethod (packages/accounts-base/accounts_server.js:248:14)
    at AccountsServer.Ap._loginMethod (packages/accounts-base/accounts_server.js:381:5)
    at Object.createUser (packages/accounts-password/password_server.js:1026:19)
    at Test.<anonymous> (server/accountsMethods.tests.js:15:16)
    at run (packages/practicalmeteor:mocha-core/server.js:34:29)
    at Context.wrappedFunction (packages/practicalmeteor:mocha-core/server.js:63:33)

// 11
    var createUser = Meteor.server.method_handlers['createUser'];    // 12
    var email = '[email protected]';                                  // 13
    var password = '12345';                                          // 14
    var userId = Random.id();                                        // 15
    createUser.apply({ userId: userId }, [email, password]);         // 16
    should(Meteor.users.find({}).count()).be.exactly(1);             // 17

Upvotes: 1

Views: 3048

Answers (1)

MasterAM
MasterAM

Reputation: 16478

Because the method expects an object (although the check is performed in a callback) and you supplied 2 strings.

This is not the recommended way of invoking 3rd-party methods. See this answer for a more appropriate way of doing so.

If you insist on directly invoking the method handler, use

createUser.apply({ userId }, [{email, password}]);         

instead, which calls the handler with a single object parameter instead of 2 string parameters.

Upvotes: 0

Related Questions