Solidity
Truffle 測試在檢查合約實例是否具有所有者地址時失敗
當我對下面的智能合約進行松露測試以檢查它是否具有所有者地址時:
pragma solidity >=0.4.21 <0.7.0; contract Owner { address public owner; address public nextOwner; constructor () public { owner = msg.sender; } }
它失敗並顯示以下消息:執行truffle test之後。
Using network 'development'. Compiling your contracts... =========================== > Compiling ./contracts/Owner.sol > Artifacts written to /tmp/test-2020319-8538-1ih1ur3.6ijj > Compiled successfully using: - solc: 0.5.16+commit.9c3226ce.Emscripten.clang Contract: Owner The attributes 0x7C94D330943a4a6CC1c25eF043446dA1898F4688 ✓ has the address 1) has the owner > No events were emitted 1 passing (406ms) 1 failing 1) Contract: Owner The attributes has the owner: has owner address + expected - actual -0x0000000000000000000000000000000000000000 +0xBcB8563A1f9707BDa6bb020222b664Ddc100e5a4 at Context.it (test/DIce.js:21:20) at <anonymous> at process._tickCallback (internal/process/next_tick.js:189:7)
很明顯,實際和預期並不相等。
我使用的測試腳本如下:
const Owner = artifacts.require('./Owner.sol'); contract('Owner', accounts => { const owner = accounts[0]; const nextOwner = accounts[1]; describe('The attributes', () => { let ownerInstance; beforeEach(async () => { ownerInstance = await Owner.new(); }); it('has the address', () => { const address = ownerInstance.address; assert.notEqual(address, '0x0', 'has contract address'); console.log(address); }); it('has the owner', async () => { const address = await ownerInstance.nextOwner(); assert.equal(address, owner, 'has owner address'); }); }); });
你能建議嗎?我將不勝感激就此進行討論。謝謝。
改變這個:
ownerInstance.nextOwner()
對此:
ownerInstance.owner()
僅當契約的所有權轉移時,該
nextOwner
變數才變為非零。