[Ethereum] How to test contract with multiple accounts / addresses in truffle

addressessoliditytruffletruffle-test

I want to test my truffle contract with multiple msg.sender addresses. Like "the first user sell token, the second user buys this token". For one address I simply write something like contract.buy.value(10 wei)();. But where I could get another address and how to send money from him?

I write my tests on solidity, not on javascript.

Best Answer

Yes you can, do your test in Javascript.

In your test you can initialize it like this:

const MyContract = artifacts.require('MyContract');

contract('MyContract', accounts => {
  const owner = accounts[0];
  const alice = accounts[1];
  const bob = accounts[2];

  it('should do something', async () => {
    const contract = await MyContract.deployed();
    await contract.buy(price, data, { from: alice });
    await contract.buy(price, data, { from: bob });
    assert.equal(...);
  });
});

Notice the accounts variable. Hope this helps.

Related Topic