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.

  • Consider writing your tests in Javascript. – goodvibration Sep 2 '18 at 7:41

I am not sure if this is possible, but you can use beforeAll/beforeEach hooks in Solidity tests to get the contract to desired state before testing.

So for your example, you add a before hook and manually set the state as if contract.buy.value(10 wei)(); was called by another account beforehand.


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 });

Notice the accounts variable. Hope this helps.

  • He specifically said he's writing his tests in Solidity, not JavaScript – Lauri Peltonen Sep 2 '18 at 16:29
  • Yes but just with Solidity it's not possible that I'm aware of. Just giving another approach. – Olivers De Abreu Sep 2 '18 at 22:03

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.