Truffle-Test
如何在松露測試中訪問映射數組和合約地址
我有 3 個合約:發送者、接收者和 transferTest。由於接收者合約中聲明的地址數組,我在 transferTest 中遇到錯誤:
接收器.sol:
pragma solidity ^0.5.8; contract receiver{ address public owner; mapping(address => uint) balance; constructor () public{ owner = msg.sender; } function() external payable{ balance[owner] += msg.value;} }
轉移測試:
pragma solidity ^0.5.8; import "truffle/Assert.sol"; import "../contracts/sender.sol"; import "../contracts/receiver.sol"; contract TestTransfer{ function testTransfer() public{ sender senderObj = new sender(); receiver receiverObj = new receiver(); senderObj.transferTo(msg.sender, 10); Assert.equal(receiver.balance[receiver.owner], 9, "Received amount is not correct"); } }
我收到以下錯誤消息:
testingTransfer.sol:12:20:TypeError:在類型(契約接收者)中進行參數相關查找後,未找到成員“餘額”或不可見。Assert.equal(receiver.balance
$$ receiver.owner $$, 9, “收款金額不正確”); ^————–^ 編譯失敗。往上看。松露 v5.1.67(核心:5.1.67)節點 v10.23.3
我還使用 msg.sender 作為接收者合約的地址作為 transferTo 函式的第一個參數。請指導我如何將 msg.sender 替換為接收者合約的地址。==更新:2021 年 12 月 11 日我的問題類似於: 訪問 Solidity 映射
祖爾菲。
不是公開的
balancew
,因此無法從外部訪問mapping(address => uint) balance;
如果它是公開的,您將能夠像使用鍵作為參數的函式一樣訪問。
mapping(address => mapping(address => uint256)) public allowance;
從松露的控制台
> let allow = await token.allowance("0x1234..", "0x4444...")
的固定版本
TestTransfer
應該看起來像contract TestTransfer { function testTransfer() public{ sender senderObj = new sender(); receiver receiverObj = new receiver(); senderObj.transferTo(msg.sender, 10); Assert.equal(receiverObj.balance(receiverObj.owner()), 9, "Received amount is not correct"); } }