Chai:AssertionError:期望[Function]成为一个函数(测试异常时)

时间:2017-04-14 13:23:39

标签: javascript node.js chai

当我在尝试异步函数是否抛出错误时,我收到此错误AssertionError: expected [Function] to be a function

it('has invalid password', async () => {
    const fakeData = { email: userData.email, password: 'something but not the password!.' }
    expect(async () => { await authService.authenticate(fakeData) }).to.throw(errors.UnauthenticatedError)
})

结果:

AssertionError: expected [Function] to be a function
  at Assertion.assertThrows (node_modules/chai/lib/chai/core/assertions.js:1273:32)
  at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
  at doAsserterAsyncAndAddThen (node_modules/chai-as-promised/lib/chai-as-promised.js:293:29)
  at Assertion.<anonymous> (node_modules/chai-as-promised/lib/chai-as-promised.js:252:17)
  at Assertion.ctx.(anonymous function) [as throw] (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33)
  at Context.it (dist/tests/unit/auth-service.spec.js:56:20)
  at Test._mocha2.default.Test.run (node_modules/mocha-http-detect/dist/index.js:84:21)

我做错了什么?

2 个答案:

答案 0 :(得分:3)

当您尝试传递异步函数时,这是一个已知问题。

要解决此问题,您可以使用Chai canary。

更多信息:https://github.com/chaijs/chai/issues/958

答案 1 :(得分:1)

如果您使用的是异步功能,您还可以通过以下方式检查它是否为/var/lib/phpmyadmin/config.inc.php

AsyncFunction