Writing tests for smart contracts
Truffle provides a solid test framework, which allows you to write test cases in both JavaScript and Solidity. Tests in a Truffle project should exist in the ./test
directory. Tests can have either a .js
or .sol
extension, based on the language they are written in. In this recipe, you will learn to write test cases for your smart contract in both JavaScript and Solidity.
Getting ready
You need to have Truffle installed on your machine to step through this recipe. You also need to have a working Ethereum network to enable Truffle to run tests on the contracts.
How to do it...
Truffle allows test cases written in both JavaScript and Solidity. JavaScript tests follow Mocha as a testing framework and Chai for assertions. For Solidity-based test cases, Truffle provides a default assertion library for you to use. Let's look into each method and write some test cases.
Writing tests in JavaScript
- The structure of a JavaScript-based test is very similar to a Mocha-based...