Applying Test-Driven Development On Your Truffle Dapp
Test-Driven Development(TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved so that the tests pass. (You write test cases before implementing features)
Install Truffle
npm i truffle
Download truffle Box
truffle unbox yehia67/HelloWorldBox
Then go to test/Simple.test.js
You Should find this content
const simple = artifacts.require(‘./Simple.sol’)contract(‘Simple’, (accounts) => {before(async() => {this.Simple = await simple.deployed()})it(‘deploys successfully’, async() => {const address = await this.Simple.addressassert.notEqual(address, 0x0)assert.notEqual(address, ‘’)assert.notEqual(address, null)assert.notEqual(address, undefined)})it(‘Set Function work well’, async() => {const simple = await this.Simple.set(“Simple Contract”)const Hello = await this.Simple.hello()assert.equal(Hello, “Simple Contract”)})it(‘Get Function work well’, async() => {const simple = await this.Simple.set(“Simple Contract”)const Hello = await this.Simple.get()assert.equal(Hello, “Simple Contract”)})})
Code Explanation
First lines you initialize the contract and deploy it. How?
Import the contract:
const simple = artifacts.require(‘./Simple.sol’)
Deploy Contract using contract
function with the first parameter the contract name and second parameter a call back function to import address accounts.
contract(‘Simple’, (accounts) => {before(async() => {this.Simple = await simple.deployed()})
//place test cases here
Now we are ready to create our test cases since we had this.Simple
object we can call any function or variable using it. For example, if we have a function named myfunction
we could call it like this await this.Simple.myfunction ()
To make test cases we will use await
keyword. In order to use await
it should be inside async
function. So Test cases will be like that
// 3 test cases// Check if contact deployed successfully
it(‘deploys successfully’, async() => {const address = await this.Simple.addressassert.notEqual(address, 0x0)assert.notEqual(address, ‘’)assert.notEqual(address, null)assert.notEqual(address, undefined)})//Check if Set Function working correctly
it(‘Set Function work well’, async() => {const simple = await this.Simple.set(“Simple Contract”)const Hello = await this.Simple.hello() //check hello variable assert.equal(Hello, “Simple Contract”)})//Check if Get Function working correctlyit(‘Get Function work well’, async() => {const simple = await this.Simple.set(“Simple Contract”)const Hello = await this.Simple.get()assert.equal(Hello, “Simple Contract”)})
Now, return to our goal how we will apply TDD? On this simple example, you could easily do the following
1- Decide how many contracts I am going to create (on our case it’s only one)
2-How many functions do you want to create (only 2)
3-Create file for each contract.
4-Write test cases as the example above.
5-run this command truffle test ./path/to/test/fileName.test.js
And that it you can write your test cases then develop your contact then test each one. Hope this was useful. Thank you, If you have any question feel free to ask.