我正在尝试使用"松露试验";但它显示以下错误:"TypeError:成员";所有者;在合同Gaming中的参数相关查找后未找到或不可见";owner=gaming.owner((
游戏.sol
pragma solidity ^0.5.0;
contract Gaming {
/* Our Online gaming contract */
address owner;
bool public online;
struct Player {
uint wins;
uint losses;
}
mapping (address => Player) public players;
constructor() public payable {
owner = msg.sender;
online = true;
}
modifier isOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
}
TestGaming.sol
pragma solidity ^0.5.0;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";
contract TestGaming {
uint public initialBalance = 10 ether;
Gaming gaming;
address owner;
function beforeAll() public {
gaming = Gaming(DeployedAddresses.Gaming());
owner = gaming.owner();
}
function testWithdrawFunds() public {
uint ownerBalanceBefore = owner.balance;
gaming.withdrawFunds();
uint ownerBalanceAfter = owner.balance;
Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}
在solidity中,默认可见性为internal
。对于这个代码
address owner;
它的可见性是内部的。Solidity为公共变量分配getter。
address public owner;
你可以很容易地测试混音:
contract Test {
uint public firstVar=10;
uint secondVar=50;
}
如果你在remix上部署它,你会看到firstVar
的getter,但不会看到secondVar
的getter
或者您可以为所有者编写一个getter函数:
function returnOwner() public view returns (address){
return owner
}