我目前正在学习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"
// });
因此,问题一定出在使用此方法上。不确定是否是异步问题。
答案 0 :(得分:2)
我通过将web3降级为1.0.0-beta.37
解决了这一问题。似乎1.0.0-beta.51
版存在错误。