I have a contract with a mapping that maps a number to an address and I'm trying to test the contract. Here is the relevant solidity code:

function _spawnPlayer(uint256 id) external {
    require(playerIndexToOwner[id] == address(0));
    playerIndexToOwner[id] = address(this);

And here is how I'm testing that the created object is indeed assigned to the contract's address as an owner (the line line in the function).

it("Contract is assigned as the player's owner", () => {
    return BaseInstance._spawnPlayer(301).then(function (res) {
        BaseInstance.playerIndexToOwner.call(301).then((playerOwner) => {
            assert.equal(playerOwner, account, "Contract is not the spawned player's owner.")

My problem is for some reason playerOwner keeps changing every time I run the test and account is just accounts[0] so that should never change. So why does playerOwner keep changing? Is the address that deploys the contract not the same every time?

1 Answer 1


You're storing address(this), which is the address of the contract, not the address of the account that created it.

Perhaps you meant to use msg.sender, which would instead store the address of whatever called the function.


If you're interested in the address that did the initial deployment, you may want something like this in the constructor. (msg.sender in the constructor is the account that created the contract.)

 owner = msg.sender;

See also https://programtheblockchain.com/posts/2017/12/26/checking-the-sender-in-a-smart-contract/.

  • I actually meant to compare playerOwner and BaseInstace.address but yeah I was comparing the wrong variables.
    – ninesalt
    Commented Jan 11, 2018 at 19:20

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.