Test contracts
Framework
TronBox comes standard with an automated testing framework to make testing your contracts a breeze. This framework lets you write simple and manageable tests in JavaScript, for exercising your contracts from the outside world, just like your application.
Location
All test files should be located in the ./test directory. TronBox will only run test files with the .js file extensions. All other files are ignored.
Command
To run all tests, simply run:
tronbox testAlternatively, you can specify a path to a specific file you want to run, e.g.,
tronbox test ./path/to/test/file.jsUsing TRE
To test our contracts more easily, we need to connect to a blockchain network. After v3.0.0 came out, TronBox provides a TronBox Runtime Environment (TRE) private chain that can be used for testing. Please note that this blockchain is in our local system and does not connect to the TRON main network.
You can start and interact with the blockchain by using the TronBox Runtime Environment (TRE), which is designed to build a complete private chain for development on TRON.
Steps are as follows:
- Pull images from the Docker:
docker pull tronbox/tre- Start the TronBox Runtime Environment (TRE) and run the following commands:
docker run -it -p 9090:9090 --rm --name tron tronbox/treWe will see the following information:
----------------------------------
TronBox Runtime Environment v1.0.5
----------------------------------
Start the http proxy for dApps...
[HPM] Proxy created: / -> http://127.0.0.1:18190
[HPM] Proxy created: / -> http://127.0.0.1:18191
[HPM] Proxy created: / -> http://127.0.0.1:8545
[HPM] Proxy created: / -> http://127.0.0.1:8545
[HPM] Proxy created: / -> http://127.0.0.1:8060
TRE now listening on http://127.0.0.1:9090
ADMIN /admin/accounts-generation
(1) FullNode is not already.
Sleeping for 3 seconds... Slept.
(2) FullNode is not already.
Sleeping for 3 seconds... Slept.
FullNode is already.
...
Loading the accounts and waiting for the node to mine the transactions...
(1) Waiting for receipts...
Sending 10000 TRX to TEXqRygDHvE7Xe5XcnBpJeJsRCu2pB3e4U
Sending 10000 TRX to TBXGwsmz7n6QemBABmooTBvWgn86H8n7QT
Sending 10000 TRX to THaLQRHEfmYwRbVfHkL3VrpzJzUvgStJbX
Sending 10000 TRX to TTuWX3EiRSgSEX35z39dKnkGwux27E7tYe
Sending 10000 TRX to TKnbNtvgW2jnkXZApWNMGVunuirts4UaL8
Sending 10000 TRX to TSZ4ABf1nKTsTFqqBfcGDDkfcsGcdC7784
Sending 10000 TRX to TXHDhBZ9ZYHLbH1ZEKjXBmaR3eJfdgeTWE
Sending 10000 TRX to TJUiUEAbkQ7vfupB5GoJRVr6QwQBR97n45
Sending 10000 TRX to TCzMS8UDrWJ9Yf5Pk49Y7cD9C2i7Ui4Ve1
Sending 10000 TRX to TT31CTebLhcSynLxm5Ak7W9Pjnq4Mupdbb
Sleeping for 3 seconds... Slept.
(2) Waiting for receipts...
Sleeping for 3 seconds... Slept.
(3) Waiting for receipts...
Done.
Available Accounts
==================
(0) TEXqRygDHvE7Xe5XcnBpJeJsRCu2pB3e4U (10000 TRX)
(1) TBXGwsmz7n6QemBABmooTBvWgn86H8n7QT (10000 TRX)
(2) THaLQRHEfmYwRbVfHkL3VrpzJzUvgStJbX (10000 TRX)
(3) TTuWX3EiRSgSEX35z39dKnkGwux27E7tYe (10000 TRX)
(4) TKnbNtvgW2jnkXZApWNMGVunuirts4UaL8 (10000 TRX)
(5) TSZ4ABf1nKTsTFqqBfcGDDkfcsGcdC7784 (10000 TRX)
(6) TXHDhBZ9ZYHLbH1ZEKjXBmaR3eJfdgeTWE (10000 TRX)
(7) TJUiUEAbkQ7vfupB5GoJRVr6QwQBR97n45 (10000 TRX)
(8) TCzMS8UDrWJ9Yf5Pk49Y7cD9C2i7Ui4Ve1 (10000 TRX)
(9) TT31CTebLhcSynLxm5Ak7W9Pjnq4Mupdbb (10000 TRX)
Private Keys
==================
(0) 394b29b057ac61f7a24b7b69b5e1b1c5e51e5d979fed643b02a1d2cd7806bba7
(1) 2df1251d9316f83359b16045adf5da9272548ff807df1c069d5b6c453c972908
(2) 3e13e6be986671f18b27b73e64dc0ef121c5ee0215ffbc6c46d8ff168595c009
(3) 706738fb63dd4578cabe5dac2d80031ea9b716eee34101eca07cf5ee61732075
(4) 8beb98d186ca3a66896b3d9d3de6bf06c64c82a93d49b1f73faf4f114e14049e
(5) 9b4cc3c37630b41bf758109165358b26601413094eb5fd1ca126eb85093d7d85
(6) 435ea41ebaa3d305fb6cf4235febe7f59cf114130b014507e991824d4d0204f3
(7) c99c805f41a740c4457d04b0925ebf9770c196e1bf34da5b3783a4987d11850a
(8) 3de3f23ae6a7ba1cf3d9bc796f4afc60e02d7a2c18f8530e23fc8600f3efac58
(9) fc314b236822e65b37fc2cdf9572b72f72940e8a18c7952eb45afc743a2a2be9
HD Wallet
==================
Mnemonic: exclude wall afford color victory good often rose income ranch evoke north
Base HD Path: m/44'/195'/0'/0/{account_index}
GET 200 - 10752.337 msHere we see ten accounts (and their private keys) that can be used to interact with the blockchain.
TIP
NOTE: This Docker image is not optimized for the Apple ARM chip (Apple silicon), so it may be unusable and the Docker needs to be restarted several times.
- Open the console and execute the deployment:
tronbox migrateWe will see the following output:
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
Migrations:
(base58) TVbadXqMTap8D2rNjyf353Rc2jD82kWKac
(hex) 41d74b6b5c04a4a8ec9dd60127d354540d40270889
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ConvertLib...
ConvertLib:
(base58) TGcA7gVte4sw45FVXg7tkA7Y4MVYKWvgT5
(hex) 4148cdbc805388241d397bc9e3da63cc1371e21b30
Linking ConvertLib to MetaCoin
Deploying MetaCoin...
MetaCoin:
(base58) TLQnf4xrHjn2t983AutNZKzJwisJEyM18k
(hex) 417287c03e5db15b58b8d3c111302815b6d10c0235
Saving successful migration to network...
Saving artifacts...Here shows the network, addresses of your deployed contracts, and transactions statuses.
After starting TronBox Runtime Environment (TRE), you can use the following advanced features in unit testing.
TIP
NOTE: You can use tronWrap in unit testing.
tre_setAccountBalance: To set account balance.
const address = 'TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL';
const balance = '0x3e8';
const result = await tronWrap.send('tre_setAccountBalance', [address, balance]);
console.log(result);tre_setAccountCode: To set account code (It's recommended to set it as runtime bytecode, because setting it to the creation code of the contract will cause an exception in executing the contract).
const address = 'TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL';
const data = '0xbaddad42';
const result = await tronWrap.send('tre_setAccountCode', [address, data]);
console.log(result);tre_setAccountStorageAt: To set account storage.
const address = 'TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL';
const slot = '0x0000000000000000000000000000000000000000000000000000000000000005';
const data = '0xbaddad42';
const result = await tronWrap.send('tre_setAccountStorageAt', [address, slot, data]);
console.log(result);tre_mine: Immediately mine the specified number of blocks, and the blocks parameter specifies the additionally mined blocks. The parameter range is (0, 100] (if the parameter is not given, the default is 1).
const result = await tronWrap.send('tre_mine', [{ blocks: 5 }]);
console.log(result);tre_unlockedAccounts: Unlock accounts. Transactions sent from unlocked accounts do not require private key signatures.
const result = await tronWrap.send('tre_unlockedAccounts', [['TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL']]);
console.log(result);tre_blockTime: To set the auto-mining time. If the parameter is 0, auto-mining is stoped, and a block will mine after receiving a transaction.
const result = await tronWrap.send('tre_blockTime', [3]);
console.log(result);
