如何解决错误““之前”的钩子:准备套件:“

时间:2019-09-22 03:01:58

标签: testing ethereum solidity truffle

当我测试合同时,出现了错误““之前”的钩子:准备套件:“

有人知道如何解决此错误吗?

这是我的依赖。 “松露”:“ 5.0.7”, “ web3”:“ 1.0.0-beta.46”

2 个答案:

答案 0 :(得分:1)

npm卸载-g松露

npm install -g truffle@5.1.10(总是让我回到正轨)

答案 1 :(得分:1)

此外,当使用几个测试套件时会出现此问题,这些测试套件使用没有await的异步测试助手(例如,OpenZeppelin Test Helpers: expectEvent, expectRevert)。

考虑示例:

合同

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

contract Test03 {
    address public owner;
    int32 public id;

    event ContractCreated();

    constructor() {
        owner = msg.sender;

        emit ContractCreated();
    }

    function doSmth(int32 _id) public {
        require(id != 0, "id is zero");
        id= _id;
    }
}

测试

const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');

const TestContract = artifacts.require('Test03');

contract('Test03', function (accounts) {
    const [owner] = accounts;
    const txParams = { from: owner };

    beforeEach(async function () {
        this.testContract = await TestContract.new(txParams);
    });

    describe('construction', function () {
        it('initial state', async function () {
            expect(await this.testContract.owner()).to.equal(owner);

            // !! DONT FORGET await before expectEvent-call <<------------------------------
            await expectEvent.inConstruction(this.testContract, 'ContractCreated');
        });
    });

    describe('doSmth', function () {
      it('fail when passed zero id', async function () {

        // !! DONT FORGET await before expectRevert-call <<------------------------------
        await expectRevert(
          this.testContract.doSmth(0, txParams),
          "id is zero");
      });
  });    
});

package.json

{
..
  "devDependencies": {
    "@openzeppelin/test-helpers": "^0.5.10"
  }
..
}