首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AssertionError:期望[函数]是一个函数(在测试异常时)

AssertionError:期望[函数]是一个函数(在测试异常时)
EN

Stack Overflow用户
提问于 2017-04-14 13:23:38
回答 2查看 9.4K关注 0票数 9

当我尝试一个异步函数抛出一个错误时,我得到了这个错误AssertionError: expected [Function] to be a function

代码语言:javascript
复制
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)
})

结果:

代码语言:javascript
复制
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)

我做错什么了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-14 13:27:57

这是一个已知的问题,当您试图传递一个异步函数时会发生这个问题。

要解决此问题,您可以使用柴金丝雀。

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

票数 9
EN

Stack Overflow用户

发布于 2018-04-08 14:00:54

如果您使用的是异步函数,您还可以通过以下方式检查它是否为AsyncFunction

代码语言:javascript
复制
const myAsyncFunc = async () => {};

expect(myAsyncFunc).to.be.a('AsyncFunction');
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43412458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档