0

I am currently following my first Ethereum course, and I am trying to test the a very simple contract, but it keeps failing. When I run the contract on Remix, everything seems to be working fine, so I'm assuming it's the test setup. I would appreciate any input.

The contract:

pragma solidity ^0.4.17;

contract Loaner {
    address public lender;
    address public borrower;
    
    function Lottery() public {}

    function setLender() public {
        lender = msg.sender;
    }

    function setBorrower() public {
        borrower = msg.sender;
    }
}

My test code:

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 loaner;
let accounts;

before(async () => {
    accounts = await web3.eth.getAccounts();
    loaner = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode })
    .send({ from: accounts[0], gas: '1000000' });
})

describe('Loaner', () => {
    it('deploys a contract', () => {
        assert.ok(loaner.options.address);
    });

    it('allows setting a lender', async () => {
        await loaner.methods.setLender().call({ from: accounts[0] });
        const lender = await loaner.methods.lender().call({ from: accounts[0] });
        assert.equal(lender, accounts[0]);
    });

    it('allows setting a borrower', async () => {
        await loaner.methods.setBorrower().call({ from: accounts[0] });
        const borrower = await loaner.methods.lender().call({ from: accounts[0] });
        assert.equal(borrower, accounts[0]);
    });
})

Results:

Loaner
    ✔ deploys a contract
    1) allows setting a lender
    2) allows setting a borrower


  1 passing (108ms)
  2 failing

  1) Loaner
       allows setting a lender:

      AssertionError [ERR_ASSERTION]: '0x0000000000000000000000000000000000000000' == '0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C'
      + expected - actual

      -0x0000000000000000000000000000000000000000
      +0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C
      
      at Context.<anonymous> (test/Lonaer.test.js:26:16)
      at processTicksAndRejections (node:internal/process/task_queues:94:5)

  2) Loaner
       allows setting a borrower:

      AssertionError [ERR_ASSERTION]: '0x0000000000000000000000000000000000000000' == '0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C'
      + expected - actual

      -0x0000000000000000000000000000000000000000
      +0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C
      
      at Context.<anonymous> (test/Lonaer.test.js:32:16)
      at processTicksAndRejections (node:internal/process/task_queues:94:5)
1
  • I updated my answer
    – Majd TL
    Commented Dec 13, 2022 at 11:48

1 Answer 1

1

You are mistaken between call and send. check this (old article but shows the difference) https://bitsofco.de/calling-smart-contract-functions-using-web3-js-call-vs-send/

And this is helpful too https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#web3-eth-contract

When you are changing the state of the contract like in the method setXXX(..) then you need to send a transaction and use send

Probably this will fix it:

    it('allows setting a lender', async () => {
        await loaner.methods.setLender().send({ from: accounts[0] });
        // wait until the tx is mined then
        const lender = await loaner.methods.lender().call({ from: accounts[0] });
        assert.equal(lender, accounts[0]);
    });

    it('allows setting a borrower', async () => {
        await loaner.methods.setBorrower().send({ from: accounts[0] });
        // wait until the tx is mined then
        const borrower = await loaner.methods.lender().call({ from: accounts[0] });
        assert.equal(borrower, accounts[0]);
    });
1
  • Very helpful, thanks a lot
    – Yotam
    Commented Dec 13, 2022 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.