How to test timestamp or other context on onchain views

The following question has been asked on Telegram:

Hello everyone! Is there a way to have a timestamp when unit testing an onchain_view. For entrypoints, one can run it like:
contract.entrypoint(parameter).run(now=sp.timestamp(100), sender=account)

I don’t see any documentation on being able to have fake timestamps when unit testing on-chain views

1 Like

You can change the scenario now by using scenario.compute("", now=...) and then call your on-chain view.


import smartpy as sp

def main():
    class MyContract(sp.Contract):
        def __init__(self):

        def ep(self):

        def get_now(self):

if "templates" not in __name__:
    def test():
        sc = sp.test_scenario(main)
        c1 = main.MyContract()
        sc += c1
        sc.compute("", now=sp.timestamp(5))
        now = sp.View(c1, "get_now")()
        sc.verify_equal(now, sp.timestamp(5))
1 Like