1

I'm trying to test a contract getter method that returns a large array. I am testing using the web3 library in Mocha with ganache-cli provider. My contract deploys alright, but trying to call the getArray() test doesn't pass.

Contract:

    pragma solidity ^0.4.24;    
    contract myContract{
            bytes32[7500] public array;
            function getArray() public view returns(bytes32[7500]){
                return array;
            }
    }

Test:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledContract = require('../ethereum/build/myContract.json');

let accounts;
let mycontract;

beforeEach(async () => {
    accounts = await web3.eth.getAccounts();

    mycontract = await new web3.eth.Contract(
        JSON.parse(compiledContract.interface))
        .deploy({data: compiledContract.bytecode})
        .send({from: accounts[0], gas:'6000000'});

});

describe('ourPlace', () => {
    it('allows manager to retrieve array', async (done) =>{

            const array = await mycontract.methods.getArray().call({from: accounts[0], gas:'6000000'});
            const length = array.length;
            console.log(length);
            assert.equal(7500, length);

        }).timeout(15000);
});

I first tested with 'done' and '.timeout'. The log reports a proper length of 7500 despite the Promise not resolving. The error reads:

Error: Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

I have had a somewhat similar issue with Remix's Javascript VM. But, when I deploy on Remix using Injected Web3 to Rinkeby, Remix returns the large array just fine. This makes sense because it's just a call. How do I resolve this promise? I would like to be able to run the tests properly. Thanks.

Edit: Full test code. Dependencies: "fs-extra": "^6.0.1","ganache-cli":"^6.1.3","mocha": "^5.2.0","solc": "^0.4.24","web3": "^1.0.0-beta.34"

  • Something doesn't quite make sense here. Are you using Truffle for testing (i.e., truffle test)? If yes, then your code seems to be using web3 v1.0, while Truffle currently works with web3 v0.18.4. The syntax in these two versions is completely different, so I am left to assume that you're not using Truffle for testing. How else are you testing it? Directly with node? – goodvibration Jun 24 '18 at 7:26
  • In any case, I've changed your code to use the syntax of web3 v0.18.4, and then tested it with Truffle. It works fine. The done parameter is no longer necessary, but Truffle (or Mocha) doesn't seem to mind it being passed to the it. Perhaps this is no longer the case with web3 v1.0, so try to remove it in your code. – goodvibration Jun 24 '18 at 7:26
  • 1
    Hi @goodvibration thanks for the comment. That's a rookie mistake on my part not putting version numbers. I think I am testing directly with node then based on your comments and definitely web3 v1.0 as I really don't know much about promises and callbacks. Will edit question with more info. – Duncan Brain Jun 24 '18 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.