1

I have a wrapper contract that instantiates and calls another contract, and my test cases say the owners (msg.sender)s don't match. Why?

contract A

pragma solidity ^0.4.4;

contract A {

    address public owner;

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

    function createB(string name) {
        return new B(msg.sender, name);
    }
}

contract B

pragma solidity ^0.4.4;

contract B {
    address public owner;
    string public name;

    function B(address _owner, string _name) {
        owner = _owner;
        name = _name;
    }
}

test case

pragma solidity ^0.4.0;

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

contract TestB {
    B testB;
    A testA;

    function beforeEach() {
        A = A(DeployedAddresses.A());
        B = testA.createB("test");
    }

    function testOwnerIsSet() {
        Assert.equal(address(A), address(B), "Owner's address does not match");
    }
}

Test Results

 1) TestB testOwnerIsSet:
     Error: Owner's address does not match
      at /Users/xxx/.config/yarn/global/node_modules/truffle/build/cli.bundled.js:214233:17
      at Array.forEach (native)
      at processResult (/Users/xxx/.config/yarn/global/node_modules/truffle/build/cli.bundled.js:214231:19)
      at process._tickCallback (internal/process/next_tick.js:109:7)

EDIT

I have added the address public owner to contract A. And made owner public in contract B.

And now my TestB contract looks like this:

pragma solidity ^0.4.0;

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

contract TestB {
    B testB;
    A testA;

    function beforeEach() {
        A = A(DeployedAddresses.A());
        B = testA.createB("test");
    }

    function testOwnerIsSet() {
        address aOwner = A.owner();
        address bOwner = B.owner();
        Assert.equal(aOwner, bOwner, "Owner's address does not match");
    }
}
The Nomad
  • 7,155
  • 14
  • 65
  • 100

1 Answers1

0

Based on what you have shown I am assuming you are using truffle? And to be honest I am surprised that this compiles.

When you call,

B = testA.createB("test");

you are calling it from TestB. msg.sender will be the address of TestB.

function createB(string name) {
    return new B(msg.sender, name); // msg.sender = TestB instance address
}

This may be helpful to achieve what you are attempting.