Solidity test fails when testing onlyOwner functions

by David   Last Updated May 22, 2020 17:28 PM - source

I have a contract where some functions can be called only by the owner of the contract and I want to test it with a solidity test in truffle. The contract looks like this:

contract owned {
    address public owner;

    constructor() public {
        owner = msg.sender;

    modifier onlyOwner {
        require(msg.sender == owner);

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;

contract MyContract is owned{

    function doSomething() onlyOwner public {
       // do something ..

I have setup truffle to connect to a Ganache blockchain that has only one address A. I have also verified that the address saved in owner matches the only address A. The problem is that when I try to test any of the functions that can be run only by an owner my test fails and I get the error Error: Returned error: VM Exception while processing transaction: revert. In my test I call the functions like this myContract.doSomething(params..);. Why does the transaction fail even if I have only one address in my blockchain? How can I solve this?

Answers 1

Sorry, i can't leave a comment because i don't have enough reputation points.

  1. Have you checked if the revert is caused by an out of gas ?
  2. Your contract is executed directly by you (as a msg.owner) ? because if you execute the function from another contract you are the tx.origin, so msg.owner is the in the middle.
  3. Make a function that return msg.owner and the owner, and compare them.
  4. Try to use case sensitive address (camelcase) on 3rd party softwares, that was one of my problems when dealing with python.
  5. Please share the code with us to see it.
April 02, 2020 21:31 PM

Related Questions

function assigning wrong variable

Updated November 12, 2018 10:28 AM

ParserError compiling Smart Contract, what's wrong?

Updated December 30, 2018 23:28 PM

How use Truffle Framework

Updated March 26, 2019 04:28 AM