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.
MetaCoin
doesn't have atest
method, it should fail always. Did you meanchange
instead oftest
? In such case can you put the exact error message from truffle?meta.test();
is incorrect because Metacoin doesn't have atest()
method.