Solidity
Truffle Test AssertionError:錯誤消息必須包含 revert
我無法弄清楚為什麼會發生此錯誤
這是我的測試js程式碼:
var YarockToken = artifacts.require("./YarockToken.sol"); contract('YarockToken', function(accounts) { var tokenInstance; it('initializes the contract with the correct values', function() { return YarockToken.deployed().then(function(instance) { tokenInstance = instance; return tokenInstance.name(); }).then(function(name) { assert.equal(name, 'Yarock Token', 'has the correct name'); return tokenInstance.symbol(); }).then(function(symbol) { assert.equal(symbol, 'YAROCK', 'has the correct symbol'); return tokenInstance.standard(); }).then(function(standard) { assert.equal(standard, 'Yarock Token v1.0', 'has the correct standard'); }); }) it('allocates the initial supply upon deployment', function() { return YarockToken.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 account'); }); }); it('transfers token ownership', function() { return YarockToken.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'); }) }) });
這裡是我的契約程式碼:
pragma solidity ^0.5.16; contract YarockToken { string public name = "Yarock Token"; string public symbol = "YAROCK"; string public standard = "Yarock Token v1.0"; uint256 public totalSupply; event Transfer( address indexed _from, address indexed _to, uint256 _value ); event Approval( address indexed _owner, address indexed _spender, uint256 _value ); mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; constructor(uint256 _initialSupply) public { balanceOf[msg.sender] = _initialSupply; totalSupply = _initialSupply; } function transfer(address _to, uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -= _value; } }
這是我得到的錯誤
Contract: YarockToken ✓ initializes the contract with the correct values (238ms) ✓ allocates the initial supply upon deployment (123ms) 1) transfers token ownership > No events were emitted 2 passing (541ms) 1 failing 1) Contract: YarockToken transfers token ownership: AssertionError: error message must contain revert at /Users/Kantemirovs/token_sale/test/YarockToken.js:39:7 at processTicksAndRejections (internal/process/task_queues.js:93:5)
有人可以請教我嗎?
我遇到了同樣的問題,我在 Github 上找到了解決方案。基本上,您需要在 error.message 之後刪除 indexOf() 函式,所以它應該看起來像這樣
assert(error.message, 'error message must contain revert');
這是你的答案: https ://github.com/dappuniversity/token_sale/issues/27
我剛剛完成了我的傳遞函式並通過了測試。
//Throw if caller doesn't have funds //Trigger transfer Event //return boolean function transfer(address _to, uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; return true; }
測試實施:
it("transfers tokens ownership", function() { return HanuToken.deployed().then(function(tok) { var token = tok; return token.transfer.call(accounts[1], 99999999999999); }).then(assert.fail).catch(function(error) { assert(error.message.toString().indexOf('revert') >=0, 'error msg must contains revert'); }); });