简体   繁体   English

为什么 mocha 测试在钩子 <“before each” ....> 中多次调用“错误完成()”?

[英]Why is mocha test throwing "Error done() called multiple times in hook <"before each" ....>?

I am learning about smart contracts and blockchain development.我正在学习智能合约和区块链开发。 I am currently trying to test the deployment of a simple contract using mocha but when running the test the following error is thrown :我目前正在尝试使用 mocha 测试简单合同的部署,但是在运行测试时会引发以下错误:

Error: done() called multiple times in hook <"before each" hook for "deploys contract"> (of root suite) at createMultipleDoneError (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/errors.js:428:13) at multiple (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:290:24) at done (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:301:14) at /Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:371:11 at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_MOCHA_MULTIPLE_DONE', valueType: 'undefined', value: undefined错误:在 createMultipleDoneError (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/errors.js:428) 中多次调用 done() :13) 在多个 (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:290:24) 完成 (/Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js :301:14) 在 /Users/nacholopez/Desktop/Inbox/node_modules/mocha/lib/runnable.js:371:11 在 processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_MOCHA_MULTIPLE_DONE',值类型:'未定义',值:未定义

Here is my compilation and testing code:这是我的编译和测试代码:

compile.js:编译.js:

const path = require('path');
const fs = require('fs');
const solc = require('solc');
const Inboxpath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(Inboxpath, 'utf8');

const input = {
    language: "Solidity",
    sources: {
        "Inbox.sol": {
            content: source,
        },
    },
    settings: {
        outputSelection: {
            "*": {
                "*": ["*"],
            },
        },
    },
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));

module.exports = output.contracts["Inbox.sol"].Inbox;

Inbox.test.js :收件箱.test.js:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { abi, evm } = require('../compile');

let accounts;
let inbox;

beforeEach(async () => {
    accounts = await web3.eth.getAccounts();
    inbox = await new web3.eth.Contract(abi).
        deploy({ data: evm.bytecode.object, arguments: ['hi there'] }).
        send({ from: accounts[0], gas: '1000000' });
});

describe('Inbox', () => {
    it('deploys contract', () => {
        console.log(inbox);
    });
});

I ran into the same issue as well and I made the callback to the 'it' function 'async' and that fixed the issue for me.我也遇到了同样的问题,我对“it”函数“异步”进行了回调,这为我解决了这个问题。

it("deploys a contract", async () => {
        assert.ok(inbox.options.address);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM