5

I'm consulting truffle's docs on how to test contracts directly in solidity.

http://truffleframework.com/docs/getting_started/solidity-tests

I'd like to figure out a way to refer to existing testrpc accounts in my tests. Something like:

pragma solidity ^0.4.11;

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

contract TestBAToken {
  function testNewBATokenNotFinalized() {
    address companyFundAddress = addresses[1];  // <-- I'd like this to be a predefined testrpc account
    address userFundAddress = addresses[2];
    uint256 startBlock = block.number - 100;
    uint256 endBlock = block.number + 100;
    BAToken ba = new BAToken(companyFundAddress, userFundAddress, startBlock, endBlock);
    Assert.equal(ba.isFinalized, false, "Token sale shouldn't be finalized upon initialization.");
  }
}

Is this possible? If not, anyone have any good recommendations for how to refer to existing accounts during testing?

4

You can directly add the addresses in the solidity contract instead of addresses[1] and addresses[2]. If you do not want to do that, then you can declare some variables in the contract for the addresses and intialize them as part of the constructor as shown below.

contract TestBAToken {
  address companyFundAddress;
  address userFundAddress;
  function TestBAToken(address address1, address address2) {
    companyFundAddress = address1;
    userFundAddress = address2;
  }
  function testNewBATokenNotFinalized() {
    uint256 startBlock = block.number - 100;
    uint256 endBlock = block.number + 100;
    BAToken ba = new BAToken(companyFundAddress, userFundAddress, startBlock, endBlock);
    Assert.equal(ba.isFinalized, false, "Token sale shouldn't be finalized upon initialization.");
  }
}

If you want the declare the addresses inside that function only rather than the complete contract. You can pass them as arguments for testNewBATokenNotFinalized function.

The way to implement depends on how you would like the contract to be and what it should do.

  • Thanks, but what addresses would the caller use? Just any hard-coded value? – cayblood Jun 5 '17 at 4:07
  • It depends on what companyFundAddress and userFundAddress you want to set for the new BAToken you are creating. – joifsi Jun 5 '17 at 4:13
  • 2
    The reason I'm asking is that my tests are usually running in the context of the testrpc network, which generates a set of ten accounts with ETH in them to test with. I need to somehow inject this context into many of my non-trivial tests for them to work. How do I do this when solidity has no awareness of the testrpc accounts? – cayblood Jun 5 '17 at 17:44
  • 1
    You can pass the account addresses as part of the contract constructor. As you can see constructor here has 2 parameters address1 and address2 which can be initialized. You can refer to this, to know how to deploy contracts in truffle with constructor parameters. ethereum.stackexchange.com/questions/10830/… – joifsi Jun 6 '17 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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