1

I have the following contract

contract TimeHelpers {
   function getTimestamp() internal virtual view {
       return block.timestamp;
   }
}

I have the contract that inherits from this

contract ConcreteImpl is TimeHelpers {

    function start() public {
       uint timestamp = getTimestamp();
    }
}

Now, I wanna test start function(of course, it contains other logic as well, just not including). While testing it, I need to mock getTimestamp() so that it returns what I want.

Way 1. I can write ConreteImplMock such as

contract ConcreteImplMock is ConreteImp {

    function getTimestamp() internal override returns(uint) {
        return 100;
    }
}

Problem with this way is that wherever I use TimeHelpers, in that contract, I should be adding the overriden getTimestamp which I don't like.

Way 2. I can write TimeHelpersMock that inherits from TimeHelpers.

contract TimeHelpersLock is TimeHelpers {

    function getTimestamp() internal override returns(uint) {
       return 100;
    }
}

contract ConcreteImplMock is ConcreteImpl, TimeHelpersMock {
    
    
}

Now, problem is that in the final inheritance chain, getTimestamp ends up to be in 2 base classes and I get the following error:

Derived contract must override function "getTimestamp". Two or more base classes define function with same name and parameter types.

Question: How would you approach this ?

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.