Skip to main content
added 4 characters in body
Source Link
Aniket
  • 3.5k
  • 2
  • 21
  • 43

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.change();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.change();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.change();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

added 2 characters in body
Source Link
Aydin
  • 2.1k
  • 6
  • 26
  • 41

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.testchange();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.test();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

I'm using a simplified version of a contract provided by Solidity website. My contract is as follows:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";
contract MetaCoin {
   address owner;
   uint val;    

   function MetaCoin() {
       owner= msg.sender;
  }

    modifier onlyOwner {
    require (msg.sender==owner);
    _;
  }
  function change() onlyOwner{
  val=55;
 }
 function get_owner() returns (address){
 return owner;
 }
}

And my test contract is as below:

pragma solidity ^0.4.2;

 import "truffle/Assert.sol";
 import "truffle/DeployedAddresses.sol";
 import "../contracts/MetaCoin.sol";

 contract TestMetacoin {

 function test1(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 meta.change();
 }

 function test2(){
 MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
 Assert.equal(meta.get_owner(), msg.sender, "I must be the owner");
}
}

I'm using testrpc. So first I deploy the 1st contract: "truffle deploy" and then I run the test one: "truffle test".

Problem: test2 passes but test1 cannot. If I remove "onlyOwner" from change() then test1 can pass.

Question: Why cannot test1 pass and what is the solution?


Please note that both contracts are compiled without any error.

edited title
Link
Aydin
  • 2.1k
  • 6
  • 26
  • 41

Truffle: usingWhy is my "onlyOwner" modifier onlyOwnernot running in truffle?

edited tags
Link
Aydin
  • 2.1k
  • 6
  • 26
  • 41
Loading
Source Link
Aydin
  • 2.1k
  • 6
  • 26
  • 41
Loading