1

I have modified the following code from a michalzalecki website https://michalzalecki.com/ethereum-test-driven-introduction-to-solidity/ to test address(this):

pragma solidity 0.5.16;
import "truffle/Assert.sol";
import "../contracts/Funding.sol";
import "truffle/DeployedAddresses.sol";

contract FundingTest {
  address v = 0xBFF7fd8064C448347e239FD7B7347b6D959Fb9f2;
  function testSettingAnOwnerOfDeployedContract() public {
    Funding funding = Funding(DeployedAddresses.Funding());
    //Assert.equal(funding.owner(), address(this), "An owner is different than a deployer");
    //Assert.equal(funding.owner(), msg.sender , "An owner is different than a deployer");
    Assert.equal(v, address(this), "An owner is different than a deployer");

  }
}

My contract code is:

pragma solidity 0.5.16;

contract Funding {
  address public owner;
  constructor() public {
     owner = msg.sender;
  }
}

When I run the truffle migrate, I got following addresses:

1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x59d01a9d031010d4b0cc7d0d7db87d8d7c5d2a96f9ec1117512c1ec73e578321
   > Blocks: 0            Seconds: 0
   > contract address:    0x258B8538F1dA81DC872A21f38782B548BA849efc
   > block number:        1
   > block timestamp:     1631809265
   > account:             0x05A4de8a003419dF5e51fF20D36750e990a32C1e


2_deploy_contracts.js
=====================

   Deploying 'Funding'
   -------------------
   > transaction hash:    0xbbe3ba94cc623931990c1b802ced466b7854378a89c02c5d3adf0742b8a571aa
   > Blocks: 0            Seconds: 0
   > contract address:    0xBFF7fd8064C448347e239FD7B7347b6D959Fb9f2
   > block number:        3
   > block timestamp:     1631809266
   > account:             0x05A4de8a003419dF5e51fF20D36750e990a32C1e
   > balance:             99.99291392

I am getting the following exception message when I run the truffle test:

$ truffle test
Using network 'development'.
   
    /home/zulfi/Truffle_programs/michal-unboxRem/test/FundingTest.sol:9:5: Warning: Unused local variable.
    Funding funding = Funding(DeployedAddresses.Funding());
    ^-------------^

> Artifacts written to /tmp/test--7766-a2isLlv7uM20
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



  FundingTest
    1) testSettingAnOwnerOfDeployedContract
    > No events were emitted


  0 passing (6s)
  1 failing

  1) FundingTest
       testSettingAnOwnerOfDeployedContract:
     Error: An owner is different than a deployer
      at checkResultForFailure (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:66:1)
      at process._tickCallback (internal/process/next_tick.js:68:7)

Somebody please guide how to avoid the exception, how to retrieve the correct value of address(this), why migration value does not work?

Zulfi.

2
  • You have to clarify your intent. What are you trying to achieve? address(this) returns the address of the contract itself. Commented Sep 16, 2021 at 17:42
  • Do you want to test the contract was deployed by 0xBFF7fd8064C448347e239FD7B7347b6D959Fb9f2? In that case the test should be Assert.equal(v, funding.owner(), "An owner is different than a deployer");.
    – Ismael
    Commented Sep 20, 2021 at 5:24

1 Answer 1

1

address(this) will return the address of the FundingTest contract, so of course it will be different from the Funding contract. The test should pass if you use address(funding), assuming your address for v is correct.

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.