Skip to main content

Smart Contracts Playground - Mock form

Mocks allow developers to simulate the behaviour of functions that have side-effects. A few examples of side-effect: retrieve the genesis address of a chain, retrieve the date, a random value etc.

The mock form is part of the trigger panel.

the mock form is display

These functions must be mocked to test Smart Contracts that use them:

A function can be mocked multiple times, for example to mock multiple different inputs.

Example: Time.now/0

Time.now/0 can be mocked to simulate a future execution of the contract. This one is actually optional, the playground will use current time if it is not set.

@version 1
actions triggered_by: interval, at: "* * * * *" do
Contract.set_content(Time.now())
end

All you need to do is fill the expected output (seconds since epoch):

mocking the Time.now/0

Then click on the Add button, then trigger to test the Smart Contract:

Time.now/0 is mocked

We can see via the console that the mock was called as expected.

Example: Chain.get_genesis_address/1

Let's see an example with a function that takes an argument. In the form you specify the output for a given input. Let's try it!

The screenshot below mocked the function with these values:

  • input: 00002223BBD4EC3D64AE597696C7D7ADE1CEE65C639D885450AD2D7B75592AC76AFA
  • output: MOCKED
@version 1
actions triggered_by: interval, at: "* * * * *" do
Contract.set_content(Chain.get_genesis_address(0x00002223bbd4ec3d64ae597696c7d7ade1cee65c639d885450ad2d7b75592ac76afa))
end

a contract that used Chain.get_genesis_address/1 is tested

We can see via the console that the mock was called as expected.

a contract that used Chain.get_genesis_address/1 is tested