【问题标题】:How to solve the error ""before all" hook: prepare suite:"如何解决错误“”before all“钩子:准备套件:”
【发布时间】:2019-09-22 03:01:58
【问题描述】:

当我测试我的合同时,我收到了这个错误“”before all” hook: prepare suite:”

有谁知道如何解决这个错误?

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

【问题讨论】:

标签: testing ethereum solidity truffle


【解决方案1】:

npm 卸载 -g 松露

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

【讨论】:

    【解决方案2】:

    此外,当使用 几个 使用异步测试助手而不使用 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"
      }
    ..
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多