首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:在处理事务时返回错误: VM异常:只有所有者才能调用此函数

错误:在处理事务时返回错误: VM异常:只有所有者才能调用此函数
EN

Stack Overflow用户
提问于 2022-08-11 09:42:53
回答 1查看 212关注 0票数 0

我试图使用“松露测试”测试此合同,但它显示了以下错误:

错误:在处理事务时返回错误: VM异常:只有所有者才能调用此函数--原因是:只有所有者才能调用此函数。

Gaming.sol

代码语言:javascript
复制
pragma solidity ^0.5.0;

contract Gaming {
    /* Our Online gaming contract */
    address public 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

代码语言:javascript
复制
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");
}

误差

代码语言:javascript
复制
Error: Returned error: VM Exception while processing transaction: revert only owner can call this function - - Reason given: Only owner can call this function.
EN

回答 1

Stack Overflow用户

发布于 2022-08-11 14:12:29

您可以使用其他帐户部署合同,并使用其他帐户进行测试。

请添加测试脚本

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73318573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档