在我的Javascript单元测试中,expect(true).toBeTruthy()失败

In my Javascript Unit test, expect(true).toBeTruthy() fails

本文关键字:true toBeTruthy 失败 expect 我的 Javascript 单元测试      更新时间:2023-09-26

我不明白这怎么可能。我从一个例子中得到了这个测试,它似乎不可能失败,但它是:

expect(true).toBeTruthy();

PhantomJS 2.1.1 (Windows 7 0.0.0) Controller:TrivialCtrl should be truthy FAILED
    undefined is not a constructor (evaluating 'expect(true).toBeTruthy()')
    c:/Users/Donald/workspace/karma-trivial/spec.js:25:28

这是Chai、Mocha和PhantomJS 的因果报应

expect函数通常与chai断言库一起使用:您加载了这个插件吗?

您需要:

npm install chai karma-chai --save-dev

并将karma-chai添加到您的插件列表中。

此外,查看chai文档,我不确定toBeTruthy是否是操作员:http://chaijs.com/api/bdd/.

但是,您可以使用ok:

expect(true).to.be.ok

我更换了

expect(match).toBeTruthy();

通过传统的chai

expect(match).not.be.undefined;