Given the following two contracts

contract Parent {
    mapping(uint32 => address) private Children;

    function AddChild(address _childAddress, uint32 _childID) public
        require(Children[_childID] == address(0x0), "Child already exists.");
        Child childContract = Child(_childAddress);

contract Child {
    address public ParentAddress;

    uint32 internal ID;

    function Initialise(uint32 _ID) public
        require(ParentAddress == address(0x0), "Already initialised.");

        ParentAddress = msg.sender;
        ID = _ID;

I would like to be able to write a unit test for the 'Initialise' function on the 'Child' contract, and I'd rather directly call the 'Initialise' function in the unit test than do it through the Parent contract (which is the point of unit testing).

I'm using javascript tests with Truffle. I can't find a way of having the msg.sender be some address other than the signer. Is there some way of doing this?

In other words I'd like to be able to do something like

childContract = await Child.new({ from: owner });
await childContract.Initialise(1, { from: owner, sender: parentContractAddress });

1 Answer 1


I have not tested with truffle, but as per the Truffle V7 release


you can impersonate any account and use it as a signer, I would start there.

Hope it helps

  • Hey @donoso.eth I'm not sure this is quite what I need. I still want to sign it from the original account but have the msg.sender be the Parent contract. Basically, it will be account->Parent.AddChild()->Child.Initialise() so in the Child the tx.Origin will be an account but msg.Sender will be the Parent contract.
    – ThomasS
    Commented Dec 20, 2022 at 6:41
  • 1
    I see, afaik this is not feasible unless you use another contract that calls the childcontract. but happy to learn if otherwise!
    – donoso.eth
    Commented Dec 20, 2022 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.