Use other addresses in OpenZeppelin Test Environment tests

Where is that ‘investor’ coming from on 3rd line inside ‘should accept payments’ (Simple ERC20 Crowdsale)? Can u nudge me into right direction for managing/getting accounts while testing?

1 Like

Hi @PradhumnaPancholi,

From: https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-javascript#use-contract-instead-of-describe-

  • The contract() function provides a list of accounts made available by your Ethereum client which you can use to write tests.

We are just naming some accounts for convenience in our tests:
contract('SimpleCrowdsale', function ([ creator, investor, wallet ]) {

investor is the same as accounts[1].

1 Like

Nice, I am currently trying something from docs

const { accounts, contract } = require('@openzeppelin/test-environment');

const [ owner ] = accounts;

What I am trying to do is calling a function from a different account to verify that only owner can call it. I think I should be fine following this.

Follow up question on ‘investor’ just to be sure. If I do something like

const [owner, anotherAccount] = accounts;

Then these two will be different accounts and “owner” will be first one, right?

1 Like

Hi @PradhumnaPancholi,

Please note, as you are using OpenZeppelin Test Environment, the default sender is not part of accounts.

See the documentation for details and examples: https://docs.openzeppelin.com/test-environment/0.1/getting-started#accounts and https://docs.openzeppelin.com/test-environment/0.1/api#default-sender

If you are creating a contract that is ownable, then you can specify the transaction creating an instance is from an owner or creator account. You can then test admin only functions using an other or as you said anotherAccount and use OpenZeppelin Test Helpers to check for expectRevert.

1 Like

Got it!!!

Btw, this might not belong in this thread But one of the docs says to use “contract” instead of "describe. A funny thing happened with me. I was using “contract” before using openzeppelin-test-enviroment(just testing with chai in beginning). Using ‘contract’ completely worked for me there and even now if I don’t import it from openzeppelin-testing-enviroment.

But, when I do

const { accounts, contract } = require('@openzeppelin/test-environment');

It spits out error as ‘contract is not a function’.

Not a problem for me now but I just wanted to point it out as I was curious about it.

1 Like

Sorry, my bad. I figured it out. They were referring to different things and I got confused because I was reading back and forth on different (but similar) articles.

1 Like

Hi @PradhumnaPancholi,

Glad you figured it out. For any future readers, when migrating from Truffle test to OpenZeppelin Test Environment:

From: https://docs.openzeppelin.com/test-environment/0.1/migrating-from-truffle#switching_to_test_environment
Replace all instances of Truffle’s contract function with a regular Mocha describe . You can still access the accounts array in accounts .