Search code examples
javascriptmocha.jsethereumsolidityweb3js

Solidity function is not updating the value in the file


This is my solidity code:

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

This is my compile.js code to compile solidity code:

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"];

This is my inbox.test.js file where I wrote my test.

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

Problem: When I execute the 'npm run test' command, my first 2 tests passed successfully but the 3rd test failed showing an error that 'Hi there!' not equals to "bye".

~/Documents/Inbox$ npm run test

> inbox@1.0.0 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! inbox@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the inbox@1.0.0 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

I checked everything but I don't know why that 'setMessage' function is not updating my message value in the solidity file.


Solution

  • You are calling the method with .call() instead of making transaction with .send(). This should resolve your failing test.

    await inbox.methods.setMessage("bye").send({ from: accounts[0] });