0

I have 2 functions

    function startBuilding ()
        public
    {
        uint[8] memory _buildingInfo;
        _buildingInfo[1] = uint32(block.timestamp) + 2 minutes;
        addBuilding(_buildingInfo);
    }


    function finishBuilding ()
        public
    {
        uint[8] memory _buildingInfo;
        require (buildingInfo[1] <= block.timestamp);

        buildingInfo[6] = STATUS_NEW;       
    }

Truffle Test

    it("Function 'Building' work", async function() {
        await buildingContract.startBuilding();

        // Here you need to rewind time ahead by 2 minutes

        await buildingContract.finishBuilding();
    });

How to rewind time ahead by 2 minutes to get tested require (buildingInfo [1] <= block.timestamp) ???

sleep() - will not work, because the test will be done for a very long time, because there are a lot of smart contracts like require

1 Answer 1

3

With Truffle 4.1.15 & Ganache 6.2.5, you can use this:

web3.currentProvider.send({method: "evm_increaseTime", params: [120]});

Note that:

  1. Between Truffle v4 and Truffle v5, Web3 has changed from v0 to v1, so the above might be different on Truffle 5.x (for example, I think you need to await for the function to complete).
  2. Method evm_increaseTime is 1 of 4 non-standard methods that aren’t included within the original RPC specification (i.e., it is proprietary for Ganache, see here for more details).

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.