首页 > 解决方案 > 相关测试框架- Mocha

问题描述

我正在使用以太坊平台开发简单的 DApp。在这方面,我使用了 Mocha 测试框架,但出现了这个错误。请帮忙...

E:\Tutorials\Blockchain\Practicle\Demo>npm 运行测试

demo@1.0.0 测试 E:\Tutorials\Blockchain\Practicle\Demo mocha

TypeError: Suite argument "title" must be a string. Received type "function"
    at createInvalidArgumentTypeError (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\errors.js:158:13)
    at new Suite (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\suite.js:45:15)
    at Function.Suite.create (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\suite.js:26:17)
    at Object.create (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\interfaces\common.js:128:27)
    at context.describe.context.context (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\interfaces\bdd.js:42:27)
    at Object.<anonymous> (E:\Tutorials\Blockchain\Practicle\Demo\test\inbox_test.js:13:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.exports.requireOrImport (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\esm-utils.js:20:12)
    at Object.exports.loadFilesAsync (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\esm-utils.js:33:34)
    at Mocha.loadFilesAsync (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\mocha.js:421:19)
    at singleRun (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run-helpers.js:156:15)
    at exports.runMocha (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run-helpers.js:225:10)
    at Object.exports.handler (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run.js:366:11)
    at E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\node_modules\yargs\lib\command.js:241:49
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Prabhakar's\AppData\Roaming\npm-cache\_logs\2020-06-21T05_32_41_476Z-debug.log

标签: node.jsnpmmocha.jsblockchainethereum

解决方案


我得到了我的答案。我发布它,以便对其他人有所帮助......

早些时候我将我的描述方法定义为:

describe(() => {
it("can pass the test", () => {
    console.log(accounts);
});

}); 错误图像

在这里,我通过了直接函数,而不是先去寻找标题,这就是为什么 mocha 无法成功并返回错误的原因:

套件参数“title”必须是字符串。接收类型“函数”

但是在给出我的合同方法后,它接受它作为标题

describe( 'inbox', () => {
it("can pass the test", () => {
    console.log(accounts);
});

}); 最终图像


推荐阅读