B07GL978YK EBOK by Bruno Skvorc & Ahmed Bouchefra & Mislav Javor & Tonino Jankov
Author:Bruno Skvorc & Ahmed Bouchefra & Mislav Javor & Tonino Jankov [Skvorc, Bruno]
Language: eng
Format: epub
Published: 0101-01-01T00:00:00+00:00
JavaScript Testing
Truffle enables us to use JavaScript for testing, leveraging the Mocha testing framework. This enables you to write more complex tests and get more functionality out of your testing framework.
Okay, letâs write the test. First, in the test folder, create a file and call it hashmarket.js.
The first thing we need to do, is get the reference to our contract in JavaScript. For that, weâll use Truffleâs artifacts.require(...) function:
var HashMarket = artifacts.require("./HashMarket.sol");
Now that we have the reference to the contract, letâs start writing tests. To start, weâll use the contract function provided to us:
contract("HashMarket", function(accounts) { });
This creates a test suite for our contract. Now for testing, we use Mocha it syntax:
contract("HashMarket", function(accounts) { it("should add a new product", function() { }); });
This describes the test weâll write and presents a message for us to know the purpose of the test. Now letâs write the test itself. At the end, the hashmarket.js file should look like the following. The reasoning is explained in the comments of the source code:
var HashMarket = artifacts.require("./HashMarket.sol"); contract("HashMarket", function(accounts) { it("should add a new product", function() { // Set the names of test data var itemName = "TestItem"; var itemPrice = 1000; var itemSeller = accounts[0]; // Since all of our testing functions are async, we store the // contract instance at a higher level to enable access from // all functions var hashMarketContract; // Item ID will be provided asynchronously so we extract it var itemID; return HashMarket.deployed().then(function(instance) { // set contract instance into a variable hashMarketContract = instance; // Subscribe to a Solidity event instance.ItemAdded({}).watch((error, result) => { if (error) { console.log(error); } // Once the event is triggered, store the result in the // external variable itemID = result.args.itemID; }); // Call the addNewItem function and return the promise return instance.addNewItem(itemName, itemPrice, {from: itemSeller}); }).then(function() { // This function is triggered after the addNewItem call transaction // has been mined. Now call the getItem function with the itemID // we received from the event return hashMarketContract.getItem.call(itemID); }).then(function(result) { // The result of getItem is a tuple, we can deconstruct it // to variables like this var [name, price, seller, status] = result; // Start testing. Use web3.toAscii() to convert the result of // the smart contract from Solidity bytecode to ASCII. After that // use the .replace() to pad the excess bytes from bytes32 assert.equal(itemName, web3.toAscii(name).replace(/ /g, ''), "Name wasn't properly added"); // Use assert.equal() to check all the variables assert.equal(itemPrice, price, "Price wasn't properly added"); assert.equal(itemSeller, seller, "Seller wasn't properly added"); assert.equal(status, 0, "Status wasn't properly added"); }); }); });
Run truffle test and you should get something like this:
TestHashMarket â testAddingNewProduct (109ms) Contract: HashMarket â should add a new product (64ms) 2 passing (876ms)
Your test has passed and you can be confident about not having regression bugs.
For homework, write tests for all other functions in the contract.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26588)
The Fifty Shades Trilogy & Grey by E L James(19077)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19056)
Shot Through the Heart by Mercy Celeste(18934)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17107)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16980)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16869)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16817)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16440)
The Subtle Art of Not Giving a F*ck by Mark Manson(14351)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14128)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13616)
Scorched Earth by Nick Kyme(12766)
Drei Generationen auf dem Jakobsweg by Stein Pia(10962)
Suna by Ziefle Pia(10886)
Scythe by Neal Shusterman(10333)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9519)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9365)
This is Going to Hurt by Adam Kay(9180)
