Sandbox Testing
In the previous section, we went through the contract's code, analyzing how it worked. Now, we need to test it and make sure it works as expected! For contracts, there are two types of testing you can do: unit testing and sandbox testing.
Here, we will focus on sandbox testing, as it enables one to deploy the contract in a realistic environment, allowing us to create multiple accounts and interact with the contract as if it was deployed on the blockchain.
Unit tests are built into the language and are used to test the contract functions individually. These tests work well when little context is required. However, they cannot test chain interactions - like sending accounts $NEAR tokens - since they need to be processed by the network.
Account Creation
The first thing our test does is to create multiple accounts with 10 $NEAR tokens each and deploy the contract to one of them.
- 🌐 JavaScript
- 🦀 Rust
Loading...
To deploy the contract, we pass the path to the compiled WASM contract as an argument to the test in package.json
. Indeed, when executing npm run test
, the command will first compile the contract and then run the tests.
Loading...
Notice that the sandbox compiles the code itself, so we do not need to pre-compile the contract before running the tests.
Contract Initialization
To initialize, the contract's account calls itself, invoking the init
function with an end_time
set to 60 seconds in the future.
- 🌐 JavaScript
- 🦀 Rust
Loading...
The contract measures time in nanoseconds, for which we need to multiply the result of Date.now()
(expressed in milliseconds) by 10^6
Loading...
The contract measures time in nanoseconds, for which we need to multiply the result of Utc::now().timestamp()
(expressed in seconds) by 10^9
Notice that the time is passed as a String
to the contract, this is because smart contracts cannot receive numbers larger than 52 bits
and we want to pass a unix timestamp
in nanoseconds