Truffle-Test
修復我的測試的事件聲明
我會試著把它簡單化。這是我的契約程式碼:
pragma solidity ^0.5.0; contract Lottery { address public manager; address[] public players; event Deposit(address _from, uint value); constructor() public { manager = msg.sender; } function enter() public payable { require(msg.value > .01 ether, 'Not enough ether'); players.push(msg.sender); } function numberPlayers() public view returns(uint) { return players.length; } }
這是我的測試程式碼:
const lottery = artifacts.require('Lottery'); contract('Lottery', accounts => { let instance; beforeEach( async () => { instance = await lottery.deployed(); }); it('should have as manager the account which deployed', async () => { const manager = await instance.manager.call(); assert.equal(manager, accounts[0]); }); it('should create a player if the player send some ether', async () => { const payment = web3.utils.toWei("0.02", "ether"); await instance.enter.sendTransaction({from: accounts[1], value: payment}); const numberPlayers = await instance.numberPlayers.call(); const player = await instance.players(numberPlayers - 1); assert.equal(player, accounts[1]); }) });
一切正常,但如果我像這樣在第 6 行評論存款事件:
pragma solidity ^0.5.0; contract Lottery { address public manager; address[] public players; // event Deposit(address _from, uint value); constructor() public { manager = msg.sender; } function enter() public payable { require(msg.value > .01 ether, 'Not enough ether'); players.push(msg.sender); } function numberPlayers() public view returns(uint) { return players.length; } }
我收到下一個錯誤:
1) Contract: Lottery should create a player if the player send some ether: Error: Invalid number of parameters for "enter". Got 1 expected 2! at Object.InvalidNumberOfParams (/Users/adrian.ferrereses/.nvm/versions/node/v10.15.3/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/~/web3-core-helpers/src/errors.js:32:1) at Object._createTxObject (/Users/adrian.ferrereses/.nvm/versions/node/v10.15.3/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/src/index.js:699:1) at constructor.detectNetwork.then.network (/Users/adrian.ferrereses/.nvm/versions/node/v10.15.3/lib/node_modules/truffle/build/webpack:/packages/truffle-contract/lib/execute.js:170:1) at process._tickCallback (internal/process/next_tick.js:68:7)
我是solidity 的新手,所以我不知道我是否做錯了什麼,或者我是否在solidity 編譯器的0.5 版中遇到了錯誤。
這些是我的環境的版本:
Truffle v5.0.15 (core: 5.0.15) Solidity v0.5.0 (solc-js) Node v10.15.3 Web3.js v1.0.0-beta.37
如果你想自己測試這個問題,你可以從這個儲存庫下載程式碼: https ://github.com/ardiadrianadri/lotter-project
非常感謝大家的時間
我註釋掉了這個
Deposit
事件然後跑了truffle test --network develop
。結果很好:Compiling your contracts... =========================== > Compiling ./contracts/Migrations.sol > Compiling ./contracts/lottery.sol > Artifacts written to /var/folders/ch/c9xd4bp91c5blg80llj8ytvc0000gn/T/test-119416-6547-11frumz.via4 > Compiled successfully using: - solc: 0.5.0+commit.1d4f565a.Emscripten.clang Contract: Lottery ✓ should have as manager the account which deployed ✓ should create a player if the player send some ether (92ms) 2 passing (143ms)
這很可能是一些奇怪的松露怪癖——它們經常發生。請嘗試刪除您的
build
目錄並再次執行測試。