Solidity

執行松露測試的斷言錯誤

  • March 16, 2022

我似乎無法理解這個錯誤來自哪裡。也許有人可以幫忙

我的令牌可靠性文件:

pragma solidity >=0.4.2 <0.6.0;

   contract LairToken {
   //Name
   string public name = "Lair Token";
   //Symbol
   string public symbol = "LAIR";
   string public standard = "Lair Token v1.0";
   uint256 public totalSupply;

   mapping(address => uint256) public balanceOf;

   constructor (uint256 _initialSupply) public {
       balanceOf[msg.sender] = _initialSupply;

       totalSupply = _initialSupply;
       // allocate the initial supply
   }

       // Transfer tokens

     function transfer(address _to, uint256 _value) public returns (bool sucess) {
       // Exception if the account doesnt have enough

       require(balanceOf[msg.sender] >= _value);
       // Return a boolean
       // Transfer Event
       // Transfer


     }

}

我的測試文件js:

var LairToken = artifacts.require("./LairToken.sol");

contract ('LairToken', function(accounts) {

var tokenInstance;

it('initializes the contract with the correct values', function() {
return LairToken.deployed().then(function(instance) {
   tokenInstance = instance;
   return tokenInstance.name();
}).then(function(name) {
assert.equal(name, 'Lair Token' , 'has the correct name');
return tokenInstance.symbol();
}).then(function(symbol) {
assert.equal(symbol, 'LAIR', 'hast the correct symbol ');
return tokenInstance.standard();
}).then(function(standard) {
assert.equal(standard, 'Lair Token v1.0', 'has the correct standard' );
});
})


it('allocates the initial supply upon deployment', function(){
return LairToken.deployed().then(function(instance) {
   tokenInstance = instance ;
   return tokenInstance.totalSupply();
}).then(function(totalSupply) {
   assert.equal(totalSupply.toNumber(), 1000000, 'sets the total supply 
to 1,000,000');
   return tokenInstance.balanceOf(accounts[0]);
}).then(function(adminBalance) {
   assert.equal(adminBalance.toNumber(), 1000000 , 'it allocates the 
initial supply to the admin'); 

   });

});
});


it('transfers token ownership', function() {
return LairToken.deployed().then(function(instance) {
 tokenInstance = instance;
 // Test `require` statement first by transferring something larger than 
the sender's balance
 return tokenInstance.transfer.call(accounts[1], 
99999999999999999999999);
}).then(assert.fail).catch(function(error) {
 assert(error.message.indexOf('revert') >= 0, 'error message must 
contain revert');

});

});

測試時顯示的錯誤

出現的錯誤

這是因為 ganache 已經實施了一種解決方法來轉發失敗的原因,require但其他客戶端將失敗並出現正常異常。

這對 ganache 有好處,因為 truffle 能夠顯示require失敗的確切原因。但是該功能不可移植,因為其他客戶端沒有實現類似的功能。

用您的測試替換此程式碼:)

 it("transfers token ownership", function () {
       return ValleyToken.deployed()
         .then(function (instance) {
           tokenInstance = instance;
           return tokenInstance.transfer.call(accounts[1], 9999999999999999);
         })
         .then(assert.fail)
         .catch(function (error) {
           assert(error.message, "error message must contain revert");
           return tokenInstance.transfer.call(accounts[1], 250000, {
             from: accounts[0],
           });
         })
         .then(function (success) {
           assert(success, true, "it returns true");
           return tokenInstance.transfer(accounts[1], 250000, {
             from: accounts[0],
           });
         })
         .then(function (receipt) {
           assert.equal(receipt.logs.length, 1, "triggers one event");
           assert.equal(
             receipt.logs[0].event,
             "Transfer",
             'should be the "Transfer" event'
           );
           assert.equal(
             receipt.logs[0].args._from,
             accounts[0],
             "logs the account the tokens are transferred from"
           );
           assert.equal(
             receipt.logs[0].args._to,
             accounts[1],
             "logs the account the tokens are transferred to"
           );
           assert.equal(
             receipt.logs[0].args._value,
             250000,
             "logs the transfer amount"
           );
           return tokenInstance.balanceOf(accounts[1]);
         })
         .then(function (reciept) {
           return tokenInstance.balanceOf(accounts[1]);
         })
         .then(function (balance) {
           assert.equal(
             balance.toNumber(),
             250000,
             "adds the amount to the recieving amount"
           );
           return tokenInstance.balanceOf(accounts[0]);
         })
         .then(function (balance) {
           assert.equal(
             balance.toNumber(),
             750000,
             "deducts the amount from the sending account"
           );
         });
     });

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