我目前正在学习可靠,并试图建立一个简单的合同。我还在尝试使用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"
// });
所以问题一定出在使用这种方法上。不确定是否为异步问题。
发布于 2019-04-11 05:38:38
我通过将web3降级为1.0.0-beta.37
解决了这个问题。看起来1.0.0-beta.51
版本有buggy。
https://stackoverflow.com/questions/55598440
复制