Web3js

未擷取的錯誤:回調已在 mocha 中呼叫

  • March 18, 2022

按照老化教程,我正在嘗試學習如何測試智能合約:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');

const web3 =  new Web3(ganache.provider());  
const { abi, bytecode} = require('../compile')



let accounts;
let simpleStorage;

beforeEach(async ()=>{
   // Get a list of all accounts
   accounts = await web3.eth.getAccounts();
   //Use one of those account to deply the contract
   simpleStorage = await new web3.eth.Contract(abi)
   .deploy({
       data: bytecode,
       arguments: ['Hi there!'] 
   })
   .send({
       from: accounts[0],
       gas: '1000000'
   });
});

describe('SimpleStorage', ()=> {
   it('deploy a contract', ()=>{
       console.log(simpleStorage);
   });
});

compile.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, 'UTF-8');

var input = {
   language: 'Solidity',
   sources: {
       'Inbox.sol' : {
           content: source
       }
   },
   settings: {
       outputSelection: {
           '*': {
               '*': [ '*' ]
           }
       }
   }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));


exports.abi = output.contracts['Inbox.sol']['SimpleStorage'].abi;
exports.bytecode = output.contracts['Inbox.sol']['SimpleStorage'].evm.bytecode.object;

和契約Inbox.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract SimpleStorage {
   uint256 public num;

   function set(uint256 _num) public {
       num = _num;
   }
   function get() public view returns (uint256) {
       return num;
   }
}

但我得到這個錯誤:

0 passing (290ms)
 1 failing

 1) "before each" hook for "deploy a contract":
    Uncaught Error: Callback was already called.
     at /home/pc/Desktop/inbox/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/merkle-patricia-tree/node_modules/async/lib/async.js:43:1
     at d.<anonymous> (node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/merkle-patricia-tree/node_modules/async/lib/async.js:358:1)
     at d.emit (node:events:390:28)
     at d.destroy (node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/node_modules/level-ws/level-ws.js:140:1)
     at finish (node:internal/streams/writable:763:14)
     at processTicksAndRejections (node:internal/process/task_queues:83:21)



/home/pc/Desktop/inbox/node_modules/mocha/lib/runner.js:965
   throw err;
   ^

Error: done() called multiple times in hook <"before each" hook for "deploy a contract"> (of root suite)
   at createMultipleDoneError (/home/pc/Desktop/inbox/node_modules/mocha/lib/errors.js:428:13)
   at multiple (/home/pc/Desktop/inbox/node_modules/mocha/lib/runnable.js:290:24)
   at done (/home/pc/Desktop/inbox/node_modules/mocha/lib/runnable.js:301:14)
   at /home/pc/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
}

版本package.json

"ganache-cli": "^6.1.8",
"mocha": "^9.1.3",
"solc": "^0.8.11",
"web3": "^1.6.1"

這個錯誤讓我困擾了好幾個小時。感謝您幫助解決它。

我看到的唯一問題是,回調it應該是async

describe('SimpleStorage', ()=> {
   it('deploy a contract',  async  ()=>{
       console.log(simpleStorage);
   });
});

一個問題是你傳遞了一個建構子參數 ( Hi there!),但你的合約不接受任何建構子參數,所以這應該是一個空數組。

編輯:實際上我無法在本地複制它。您的環境是什麼(例如節點版本、作業系統)以及您如何執行測試?你有機會分享回購嗎?

引用自:https://ethereum.stackexchange.com/questions/117435