Writing tests in JavaScript

  1. The structure of a JavaScript-based test is very similar to a Mocha-based test. The difference lies in the fact that Truffle has an extra contract() function. This method works like describe() in Mocha, except it creates a new instance of the contract:
contract('TokenContract', function() {
// Write tests
});

Truffle uses the clean room feature while running tests. This feature redeploys the contract before executing the contract() method in tests. This ensures that you have a fresh set of contracts to test against. You can still use describe() to run tests without the clean room feature.

  1. The contract() function also provides the list of accounts in the Ethereum client, which you can use to write tests:
contract('TokenContract', function(accounts) {
// Write tests
});
  1. Reference any contract using the artifacts.require() method. It works the same way as using it within the migrations:
var TokenContract = artifacts.require("TokenContract.sol");
  1. Use contract abstractions to interact with the deployed contract. Use them to make sure your contracts are working properly:
var TokenContract = artifacts.require("TokenContract.sol"); 

contract('TokenContract', function(accounts) {
it("Contract test template", function() {
return TokenContract.deployed().then(function(instance) {
console.log(instance);
});
});
});
  1. Make use of the contract instance to execute each function in the contract and then use Chai to assess these results. Consider the following simple example of checking the balance after deploying:
var TokenContract = artifacts.require("TokenContract.sol");

contract('TokenContract', function(accounts) {

it("should allocate 10000 Token to the owner account", function() {
return TokenContract.deployed().then(function(instance) {
return instance.getBalance.call(accounts[0]);
}).then(function(balance) {
assert.equal(balance.valueOf(), 100000,
"100000 wasn't in the first account");
});
});

})

  1. Write more complex tests that include multiple transactions and calls. Promise returns allow you to chain multiple transactions together, which helps in evaluating lengthy control flows:
var TokenContract = artifacts.require("TokenContract.sol");

contract('TokenContract', function(accounts) {

it("should transfer tokens correctly", function() {
var token;
var account_one_starting_balance;
var account_two_starting_balance;
var account_one_ending_balance;
var account_two_ending_balance;
var amount = 10;

return TokenContract.deployed().then(function(instance) {
token = instance;
return token.getBalance.call(accounts[0]);
}).then(function(balance) {
account_one_starting_balance = balance.toNumber();
return token.getBalance.call(accounts[1]);
}).then(function(balance) {
account_two_starting_balance = balance.toNumber();
return token.sendToken(accounts[1], amount, {
from: accounts[0]
});
}).then(function(tx) {
return token.getBalance.call(accounts[0]);
}).then(function(balance) {
account_one_ending_balance = balance.toNumber();
return token.getBalance.call(accounts[1]);
}).then(function(balance) {
account_two_ending_balance = balance.toNumber();

assert.equal(account_one_ending_balance,
account_one_starting_balance - amount,
"Amount wasn't correctly taken from the sender");
assert.equal(account_two_ending_balance,
account_two_starting_balance + amount,
"Amount wasn't correctly sent to the receiver");
});
});

});

  1. Javascript tests also support async/await notation. Here is the same test case after replacing then() statements:
var TokenContract = artifacts.require("TokenContract.sol");

contract('TokenContract', function(accounts) {

it("should transfer tokens correctly", async () => {

let amount = 10;

let token = await TokenContract.deployed();

let balance = await token.getBalance.call(accounts[0]);
let account_one_starting_balance = balance.toNumber();

balance = await token.getBalance.call(accounts[1]);
let account_two_starting_balance = balance.toNumber();

await token.sendToken(accounts[1], amount, {
from: accounts[0]
});

balance = await token.getBalance.call(accounts[0]);
let account_one_ending_balance = balance.toNumber();

balance = await token.getBalance.call(accounts[1]);
let account_two_ending_balance = balance.toNumber();

assert.equal(account_one_ending_balance,
account_one_starting_balance - amount,
"Amount wasn't correctly taken from the sender");
assert.equal(account_two_ending_balance,
account_two_starting_balance + amount,
"Amount wasn't correctly sent to the receiver");
});

});
  1. Run the tests using the truffle test command. The tests described here will produce the following output:
  1. Run a specific test script by explicitly specifying its name:
truffle test ./test/TokenContract.js

Truffle includes a web3 instance in each test file. It is configured to use the correct provider specified on the network. You can use all supported APIs, such as web3.eth.accounts, from the test file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47