【问题标题】:How to fix "Error: types/values length mismatch" in contract testing如何修复合同测试中的“错误:类型/值长度不匹配”
【发布时间】:2019-11-18 12:32:13
【问题描述】:

我正在关注 Udemy 上的以太坊 Dapp 教程。我似乎在课程早期的某个地方偶然发现了一个错误。当问题出现时,我正准备部署我的初学者合约并使用 Mocha 对其进行测试。

我在网上找到了修复,但没有修复我的。我怀疑这是关于 web3 或 solc 的版本控制。我正在使用这两个软件包的最新版本。我正在学习的教程使用的是已经过时的旧版本。

收件箱.sol

pragma solidity >=0.4.0 <0.6.0;

contract Inbox{
    string message;

    function set(string memory initialMessage) public {
        message = initialMessage;
    }
    function setMessage(string memory newMessage) public{
        message = newMessage;
    }
}

编译.js

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8'); 

module.exports = solc.compile(source, 1).contracts[':Inbox'];
Inbox.test.js:

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

let accounts;
let inbox;

beforeEach(async ()=>{

    accounts = await web3.eth.getAccounts();

   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)
    });
});

我的终端上的错误是:

before each" hook for "deploys a contract":
     Error: types/values length mismatch (count={"types":0,"values":1}, value={"types":[],"values":["Hi there!"]}, version=4.0.32)
      at Object.throwError (node_modules\ethers\errors.js:76:17)
      at AbiCoder.encode (node_modules\ethers\utils\abi-coder.js:922:20)
      at AbiCoder.encodeParameters (node_modules\web3-eth-abi\dist\web3-eth-abi.cjs.js:45:34)
      at MethodEncoder.encode (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:143:45)
      at MethodsProxy.createMethod (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:556:57)
      at MethodsProxy.executeMethod (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:534:23)
      at Function.ContractMethod.send (node_modules\web3-eth-contract\dist\web3-eth-contract.cjs.js:505:29)
      at Context.beforeEach (test\Inbox.test.js:19:6)
      at process._tickCallback (internal/process/next_tick.js:68:7)

我的 package.json:

{
    "name": "inbox",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "mocha"
    },
    "author": "Ryan Arcel Galendez",
    "license": "ISC",
    "dependencies": {
      "ganache-cli": "^6.4.4",
      "mocha": "^6.1.4",
      "solc": "^0.4.26",
      "web3": "^1.0.0-beta.55"
}

我希望我的 Mocha 测试显示“成功”。

【问题讨论】:

    标签: ethereum solidity web3 contract


    【解决方案1】:

    问题在于您没有接受单个参数的构造函数,这就是为什么当您部署合约实例并传入初始消息“您好!”时的原因它失败了。

    看起来你的 set 函数应该是基于参数名称的构造函数。

        function set(string memory initialMessage) public {
            message = initialMessage;
        }
    

    您应该将function set 更改为constructor

    pragma solidity >=0.4.0 <0.6.0;
    
    contract Inbox{
        string public message;
    
        constructor(string memory initialMessage) public {
            message = initialMessage;
        }
    
        function setMessage(string memory newMessage) public{
            message = newMessage;
        }
    }
    

    您可能需要查看Truffle 和/或ZeppelinOS 等工具来简化智能合约开发。

    【讨论】:

    • 谢谢。我很感激这个答案。但是发生了另一个错误:Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
    • --超时 600000
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多