以太坊 Truffle单元测试(javascript)

之前的文章以太坊 Truffle 合约创建 编译 迁移介绍了 Truffle 创建部署合约,下面在此基础上实践 Truffle javascript 版单元测试。

1. 创建测试文件

test/storage.js

var Storage = artifacts.require("Storage");

contract('Storage', function (accounts){

  // --- test get
  it('get storedData', function (){
    var storageInstance;
    return Storage.deployed().then(function (instance){
      storageInstance = instance;
      return storageInstance.get.call();
    }).then(function(storedData){
      assert.equal(storedData, 0, "storedData equal zero");
    });
  });

  // --- test set
  it('set 100 in storedData', function(){
    var storageInstance;

    return Storage.deployed().then(function (instance){
      storageInstance = instance;
      return storageInstance.set(100);
    }).then(function (){
      return storageInstance.get.call();
    }).then(function (storedData){
      assert.equal(storedData, 100, "100 wasn't in storedData");
    });
  });
});

2. 执行测试

$ truffle test
Using network 'development'.



  Contract: Storage
    ✓ get storedData
    ✓ set 100 in storedData (43ms)


  2 passing (82ms)

你可能感兴趣的:(以太坊 Truffle单元测试(javascript))