错误:匹配错误:预期对象,在测试中得到了字符串

时间:2016-12-28 08:35:28

标签: testing meteor mocha meteor-methods

大家好我正在尝试为我的流星方法编写一个测试。所以我在服务器文件夹

中创建了一个文件accountsMethods.js
import { Meteor } from 'meteor/meteor'

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

    Accounts.createUser(userObject)
  }
})

且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 = 'test@example.com'
    const password = '12345'
    const userId = Random.id()
    createUser.apply({ userId }, [email, password])
    should(Meteor.users.find({}).count()).be.exactly(1)
  })
})

在测试中我有以下错误,但我不确定这是什么。

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 = 'test@example.com';                                  // 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

1 个答案:

答案 0 :(得分:0)

因为方法expects an object(尽管检查是在回调中执行的)并且您提供了2个字符串。

这是调用第三方方法的不推荐方式。请参阅this answer了解更合适的方法。

如果您坚持直接调用方法处理程序,请使用

createUser.apply({ userId }, [{email, password}]);         
相反,它使用单个对象参数而不是2个字符串参数调用处理程序。