AssertionError: expected {} to be a function
AssertionError: expected {} to be a function
我第一次在 Chai 中使用 Mocha 时遇到了一个奇怪的错误。我以前用过的唯一框架是 Jasmine,如果它能帮助你找出我做错了什么的话。
我只是想确保我的构造函数不会引发错误。我已经能够将代码缩减为这个,但仍然出现错误:
class MyLib {
constructor() {}
}
export default MyLib;
这是测试文件:
import chai from 'chai';
let { expect } = chai;
import MyLib from '../index';
describe('mylib-js', () => {
describe('constructor', () => {
it('should work', () => {
expect(new MyLib()).to.not.throw();
})
});
});
这是摩卡的输出:
d:\path\to\mylib-js>mocha --require babel-core/register
mylib-js
constructor
1) should work
0 passing (23ms)
1 failing
1) mylib-js constructor should work:
AssertionError: expected {} to be a function
at Assertion.an (d:\path\to\mylib-js\node_modules\chai\lib\chai\core\assertions.js:169:10)
at Assertion.assert (d:\path\to\mylib-js\node_modules\chai\lib\chai\utils\addChainableMethod.js:84:49)
at Assertion.assertThrows (d:\path\to\mylib-js\node_modules\chai\lib\chai\core\assertions.js:1273:32)
at Assertion.ctx.(anonymous function) [as throw] (d:\path\to\mylib-js\node_modules\chai\lib\chai\utils\addMethod.js:41:25)
at Context.<anonymous> (test.js:10:4)
at callFn (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:315:21)
at Test.Runnable.run (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:308:7)
at Runner.runTest (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:533:12
at next (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7
at next (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5)
我不知道错误输出中的 {}
从哪里来的。它想告诉我什么?
如异常消息所述,new MyLib
是对象而非函数。
它应该像这样才能很好地与 throw
断言一起工作。
expect( () => new MyLib).not.to.throw()
我第一次在 Chai 中使用 Mocha 时遇到了一个奇怪的错误。我以前用过的唯一框架是 Jasmine,如果它能帮助你找出我做错了什么的话。
我只是想确保我的构造函数不会引发错误。我已经能够将代码缩减为这个,但仍然出现错误:
class MyLib {
constructor() {}
}
export default MyLib;
这是测试文件:
import chai from 'chai';
let { expect } = chai;
import MyLib from '../index';
describe('mylib-js', () => {
describe('constructor', () => {
it('should work', () => {
expect(new MyLib()).to.not.throw();
})
});
});
这是摩卡的输出:
d:\path\to\mylib-js>mocha --require babel-core/register
mylib-js
constructor
1) should work
0 passing (23ms)
1 failing
1) mylib-js constructor should work:
AssertionError: expected {} to be a function
at Assertion.an (d:\path\to\mylib-js\node_modules\chai\lib\chai\core\assertions.js:169:10)
at Assertion.assert (d:\path\to\mylib-js\node_modules\chai\lib\chai\utils\addChainableMethod.js:84:49)
at Assertion.assertThrows (d:\path\to\mylib-js\node_modules\chai\lib\chai\core\assertions.js:1273:32)
at Assertion.ctx.(anonymous function) [as throw] (d:\path\to\mylib-js\node_modules\chai\lib\chai\utils\addMethod.js:41:25)
at Context.<anonymous> (test.js:10:4)
at callFn (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:315:21)
at Test.Runnable.run (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:308:7)
at Runner.runTest (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:533:12
at next (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7
at next (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (C:\Users\Myuser\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5)
我不知道错误输出中的 {}
从哪里来的。它想告诉我什么?
如异常消息所述,new MyLib
是对象而非函数。
它应该像这样才能很好地与 throw
断言一起工作。
expect( () => new MyLib).not.to.throw()