我想用构造函数中的参数测试智能合约,但有错误。 这是我的智能合约和测试文件:
pragma solidity >=0.4.25 <0.7.0;
contract Test {
string public test;
constructor(string memory _test) public {
test = _test;
}
}
const Test = artifacts.require("Test");
contract('Test', (accounts) => {
it('should init', async () => {
const instance = await Test.new("test");
const result = await instance.test;
assert.equal("test", result, "info is not equals");
});
});
并记录:
Error: while migrating Test: Invalid number of parameters for "undefined". Got 0 expected 1!
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:365:1
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Migration._deploy (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:68:1)
at Migration._load (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:55:1)
at Migration.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:171:1)
at Object.runMigrations (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:150:1)
at Object.runFrom (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:110:1)
at Object.runAll (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:114:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:79:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:109:1)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/index.js:192:1)
at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:136:1)
Truffle v5.1.64 (core: 5.1.64)
Node v12.16.3
如何解决?
答案 0 :(得分:0)
您忘记传递交易参数,包括发件人(部署者)地址。
const instance = await Test.new("test");
应该
const accounts = await web3.eth.getAccounts();
const txParams = {
from: accounts[0]
};
const instance = await Test.new("test", txParams);
Web3 帐户获取器:https://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#getaccounts
有关 new()
函数的更多信息:https://www.trufflesuite.com/docs/truffle/reference/contract-abstractions#-code-mycontract-new-arg1-arg2-tx-params-code-