Skip to main content
added 118 characters in body
Source Link
Nick Johnson
  • 8.2k
  • 1
  • 29
  • 35

There are a few ways to do unit testing of Solidity contracts. The easiest, at least in my opinion, is blackbox testing with Truffle. Contracts tend to be relatively small and self-contained, so blackbox testing seems appropriate here.

Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like this:

contract('MetaCoin', function(accounts) { it("should put 10000 MetaCoin in the first account", function(done) { // Get a reference to the deployed MetaCoin contract, as a JS object. var meta = MetaCoin.deployed(); // Get the MetaCoin balance of the first account, and assert that it's 10000. meta.getBalance.call(accounts[0]).then(function(balance) { assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); }).then(done).catch(done); }); }); 

There are a few ways to do unit testing of Solidity contracts. The easiest, at least in my opinion, is Truffle.

Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like this:

contract('MetaCoin', function(accounts) { it("should put 10000 MetaCoin in the first account", function(done) { // Get a reference to the deployed MetaCoin contract, as a JS object. var meta = MetaCoin.deployed(); // Get the MetaCoin balance of the first account, and assert that it's 10000. meta.getBalance.call(accounts[0]).then(function(balance) { assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); }).then(done).catch(done); }); }); 

There are a few ways to do testing of Solidity contracts. The easiest, at least in my opinion, is blackbox testing with Truffle. Contracts tend to be relatively small and self-contained, so blackbox testing seems appropriate here.

Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like this:

contract('MetaCoin', function(accounts) { it("should put 10000 MetaCoin in the first account", function(done) { // Get a reference to the deployed MetaCoin contract, as a JS object. var meta = MetaCoin.deployed(); // Get the MetaCoin balance of the first account, and assert that it's 10000. meta.getBalance.call(accounts[0]).then(function(balance) { assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); }).then(done).catch(done); }); }); 
Source Link
Nick Johnson
  • 8.2k
  • 1
  • 29
  • 35

There are a few ways to do unit testing of Solidity contracts. The easiest, at least in my opinion, is Truffle.

Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like this:

contract('MetaCoin', function(accounts) { it("should put 10000 MetaCoin in the first account", function(done) { // Get a reference to the deployed MetaCoin contract, as a JS object. var meta = MetaCoin.deployed(); // Get the MetaCoin balance of the first account, and assert that it's 10000. meta.getBalance.call(accounts[0]).then(function(balance) { assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); }).then(done).catch(done); }); });