以太坊Solidity Contract - web3.eth.Contract send()方法的Mocha超时

问题描述 投票:1回答:2

我目前正在学习Solidity并试图建立一个简单的合同。我也在尝试使用Mocha框架在部署之前测试智能合约。测试代码如下:

const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const { interface, bytecode } = require("../compile");

const provider = ganache.provider();
const web3 = new Web3(provider);

let accounts;
let inbox;

beforeEach(async () => {
  // Get a list of all accounts
  accounts = await web3.eth.getAccounts();

  // Use one of those accounts to deploy the contract
  inbox = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({
      data: bytecode,
      arguments: ["Hi there!"]
    })
    .send({
      from: accounts[0],
      gas: "1000000"
    });

});

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

测试失败并超时:

> mocha

  Inbox
    1) "before each" hook for "deploys a contract"


  0 passing (2s)
  1 failing

  1) "before each" hook for "deploys a contract":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我注意到如果我注释掉send()参数,测试通过:

// .send({
//   from: accounts[0],
//   gas: "1000000"
// });

所以问题必须是使用这种方法。不确定它是否是异步问题。

mocha ethereum solidity smartcontracts web3
2个回答
2
投票

我通过将web3降级为​​1.0.0-beta.37解决了这个问题。似乎版本1.0.0-beta.51是越野车。

© www.soinside.com 2019 - 2024. All rights reserved.