Project: Test-driven Development of a Dapp

Sigit H. Yudanto
4 min readJun 12, 2022

--

Title: Auction Dapp with Test Script

  1. Create a truffle project for the smart contract Auction.sol from Part 1.
  2. INCREMENTALLY develop a test.js script to test the Auction.sol

Step 1: All the files you need, except Auction.sol and test.js, are provided for you in the course Resources. Recall the steps from your lessons, which are repeated here. Make sure you have access to these files/code, deploy scripts and other assets. Recall the design process.

Step 2: Follow the steps in creating a Truffle project that we learned in the lessons. Make sure you are able to initialize the project and deploy the Auction project on the test chain (truffle init, truffle compile, truffle develop, truffle migrate — reset)

Instructions for making the Auction truffle project:

cd cp CourseraDocs/Auction.zip

unzip Auction.zip

In the contracts directory replace Auction.sol with the Auction.sol you completed in Part 1.

Navigate to the Auction base directory, “truffle compile”. It should compile without any error.

Step 3: Review the template test.js in the CourseraDocs or from the course resources. This template has five test scripts out of which the code for first two are given to you complete. Review and understand them. The five tests are:

Test 1: It checks if the contract has been deployed. If yes, it returns the instance in the callback function. We just save the instance in a global variable for future use and also assert it to check if it is not null.

Test 2: This makes a call to the register function. Then, we call the getPersonDetails function to fetch the person’s details. The person’s details will be present in the callback function, and we are asserting to see if it is equal to the address we had registered.

Test 3: This a failure or negative test case (where revert should be ideally executed). We call the bid function with more than five tokens, which throws a revert error.

Test 4: This a failure or negative test case (where revert should be ideally executed). We call the revealWinners function from a non-owner account, which throws a revert error.

Test 5: This is the proper working flow of the contract. This is a positive test. We register three accounts, bid items using them, and call revealWinners from the authorized account. After which, we assert the winner of each item to see if the address is set.

Review the original copy of the test.js given to you. Identify the first two test cases in the test script and understand the script. Navigate to test directory. Create a file test.js in the test directory, but only with the first two tests (scripts for which are given). Save it. Navigate back to your Auction base directory. Execute the command “truffle test”, and you should see the two tests pass if your Auction.sol was correct and the Truffle commands were correct. You will see the two test passing as shown below:

If this does not happen, you will have to make sure your Auction.sol is correct, or your directory structure of the Dapp is as discussed in the lesson videos and demos.

Step 4: After passing the first two tests, examine the other three test scripts provided. You can now work on the three test cases incrementally, one by one. In all, the three test cases require about 19 lines of code. If you develop the script for the test case incrementally, you will focus only on that test case and therefore less code.

Now, add template code for the three test cases but comment out the last two so that you will execute only the first of the three test cases added. Apply the knowledge from the lessons and the patterns from the test code for Ballot.sol, add scripts/code at the indicated points. Save the test.js. Then, execute “truffle test” to see one more test passing if the code you added is correct. Repeat this for the other two test cases.

If all of the tests are included in the test.js and the code you entered is correct, when you execute “truffle test” you should see the following output: (Note that the timing may be different.)

Step 5: Now you are ready to submit. Do not change the template we have given so that the grader can assess the completeness of your test script. Submit test.js only for the grader to evaluate it and assign your grade.

Summary: We went through the process of testing a smart contract that is at the core of a Dapp in a blockchain-based system. The overarching goal of this course project is to emphasize thorough testing of the smart contract and to highlight the importance of testing. Never deploy any code without testing.

--

--

Sigit H. Yudanto
Sigit H. Yudanto

Written by Sigit H. Yudanto

⛓️ Blockchain Basic Certified (Coursera) University at Buffalo & State University of New York. ✉️ Inquiry: hello[at]sigit.co.uk

No responses yet