Solidity函数未更新文件中的值

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

这是我的验证码:

pragma solidity >=0.4.17;

    contract Inbox {

        string public message;

        constructor(string memory _initMessage) public {
            message = _initMessage;
        }

        function setMessage(string memory _newMessage) public {
            message = _newMessage;
        }
    }

这是我的用于编译坚固性代码的compile.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 provider = ganache.provider();
const web3 = new Web3(provider);

const { interface, bytecode } = require("../compile");

//global variables
let accounts;
let inbox;

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

  // use one of the 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" });

  inbox.setProvider(provider);
});

describe("Inbox", () => {
  it("deploys a contract", () => {
    assert.ok("inbox.option.address");
  });

  it("set initial value", async () => {
    const messsage = await inbox.methods.message().call();
    assert.equal(messsage, "Hi there!");
  });

  it("can set new value", async () => {
    await inbox.methods.setMessage("bye").call({ from: accounts[0] });
    const mess = await inbox.methods.message().call();
    assert.equal(mess, "bye");
  });
});

问题:当我执行'npm run test'命令时,我的前2个测试成功通过,但是第3个测试失败,并显示错误消息'嗨!”。不等于“再见”。

~/Documents/Inbox$ npm run test

> [email protected] test /home/sahil/Documents/Inbox
> mocha



  Inbox
(node:19409) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [l]. Use emitter.setMaxListeners() to increase limit
    ✓ deploys a contract
    ✓ set initial value
    1) can set new value


  2 passing (511ms)
  1 failing

  1) Inbox
       can set new value:

      AssertionError [ERR_ASSERTION]: 'Hi there!' == 'bye'
      + expected - actual

      -Hi there!
      +bye

      at Context.<anonymous> (test/Inbox.test.js:38:12)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `mocha`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/sahil/.npm/_logs/2020-03-30T05_58_56_753Z-debug.log

我检查了所有内容,但是我不知道为什么'setMessage'函数没有更新我的消息在Solidity文件中的值。请帮助。

mocha ethereum solidity web3
1个回答
0
投票

您正在使用.call()调用该方法,而不是使用.send()进行事务。这样可以解决您的测试失败。

await inbox.methods.setMessage("bye").send({ from: accounts[0] });
© www.soinside.com 2019 - 2024. All rights reserved.