1

I am using a simple contract

Contract code

contract Bank {
    address public owner;
    mapping(address => uint) private customerBalance;
    event fallbackCalled(address, uint256);
    event deposit(address, uint256);

    constructor() public {
        owner = msg.sender;
    }

    /** Customer Deposit payable function */
    function depositFunds(uint _value) external payable returns(bool) {
        require(_value > 0, "values not greater then zero");
        customerBalance[msg.sender] += _value;
        emit deposit(msg.sender, _value);
        return true;
    }

    /** Fetch Customer Balance */
    function getCustomerBalance() public view returns(uint) {
        return customerBalance[msg.sender];
    }

    function () external payable {
        emit fallbackCalled(msg.sender, msg.value);
    }
}

Test case --->

describe('Customer should able to deposit funds', () => {
  it('customer balance should be positive ', async () => {
    // Deposite money into Bank from account[1]  #2
    let _value = 10;
    const result = await deployedInstance.methods.depositFunds(_value).send({
      from: accounts[1]
    });

    // Fetch the balance of account[1] and it should be equal to 1
    let accountBalance = await deployedInstance.methods.getCustomerBalance().call({
      from: accounts[1]
    });

    expect(accountBalance).toBe(_value);
  })
})

Test case Result

expect(received).toBe(expected) // Object.is equality

Expected: 10
Received: "10"

**Difference:** Comparing two different types of values. Expected number but received string.

When I'm performing my unit testing that time, test case is failing caz customer balance value is coming as String from Contract call but I am supplying a number value from Testcase..

I have also check the offical documentation which also shows an exmple where they are fetching uint but getting string.

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html?#id18

Is there any specific reasons for returning strings.

Regards Dev

1

You will always get a string that contains a BigNumber because JavaScript itself has no equivalent type for an int or uint with 256 bits of precision.

They often contain interesting syntax ([]). Use toString(10) and keep in mind that loss of precision will happen. That could effect tests, but shouldn't if you use smallish numbers.

Have a look at this: https://github.com/ethereum/wiki/wiki/JavaScript-API#a-note-on-big-numbers-in-web3js

BigNumber has math methods, e.g. myNumber.add(1) so you can perform math without loss of precision. So, generally, reserve the conversion to human-readable format for UI concerns.

Hope it helps.

  • Thanks @Rob for your valuable inputs .. to make the test case working I have used the below syntex expect(parseInt(accountBalance)).toBe(_value); Or should I use BigNumber library.. is there any best practice ? – Devraj Singh Rawat Feb 7 at 12:28
  • I had tried the solution with below methods 1# expect(parseInt(accountBalance)).toBe(_value) 2# accountBalance = Number(accountBalance); expect(accountBalance).toBe(_value); Both worked as they were able to convert string to Number but I doubt these mothods will work for big number .. so I did tried with 3# expect(new BigNumber(accountBalance)).toBe(_value) But it gave me an error stating BigNumber() is not a constractor .. Exploring how to make it work.. anyone Feel free to comment further. – Devraj Singh Rawat Feb 7 at 13:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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