Truffle

部署地址的 JS 測試和 Web3 引用合約

  • September 23, 2018

我有一個契約工廠和一個子契約,現在我想測試新創建的子契約。我希望這個應該很簡單。

it("test CoinNewCoin - Test Created Coin ", function (){

   var fromOwner = accounts[0];
   var coinFactory;
   var coinNewCoin;
   return CoinFactory.deployed().then((instance) => {
       coinFactory = instance;
   }).then(() => {
   return coinFactory.getCoinAddress(fromOwner, {from: fromOwner});
   }).then((result) => {
       console.log(result);
   return CoinNewCoin.at(result).then((instance) => {
   }).then((instance) => {
       console.log(instance); // this is the undefined.
       coinNewCoin = instance;
       return coinNewCoin.getAuthorizationKey();
   })
   }).then((result) => {
       console.log(result);
   });
});

編輯:腳本更新。

return CoinNewCoin.at(result).getAuthorizationKey();

at(地址)是關鍵。謝謝-goodvibration!

it("test CoinNewCoin - Test Created Coin ", function (){

   var fromOwner = accounts[0];
   var coinFactory;
   var coinNewCoin;
   var coinAddress;
   return CoinFactory.deployed().then((instance) => {
       coinFactory = instance;
   }).then(() => {
       return coinFactory.getCoinAddress(fromOwner, {from: fromOwner});
   }).then((result) => {
       return CoinNewCoin.at(result).getAuthorizationKey();
   }).then((result) => {
       var testVal = result.toString().split(",");
       assert.equal("00000000000000000000000000000000",web3.toAscii(testVal[0]), "Coin contract is was not created, fail!")
   });
});

引用自:https://ethereum.stackexchange.com/questions/59256