You can assign a sender (as well as other transactional values) by including the data in the function call. In your specific example, you would do:
contract TestMyContract {
MyContract contract = MyContract(DeployedAddresses.MyContract());
function testCanCallFunction() public {
myContract.myFunction({from: accounts[0]);
}
}
accounts[0]
is the default deployment account. By doing this, you are calling myContract.myFunction
as the owner.
You can use any address you need in place of accounts[0]
.
While Aquila's answer can work as a workaround, the way truffleAssert.eventEmitted()
works is by applying a filter function on the event's arguments. The drawback of this is that you can't "assert" each argument individually, but this will allow you to run both assertions in this fashion.
I saw from your other question that you're using Truffle v5 with Solidity v0.5.0, so I updated your tests to use Web3 v1.0.
I used the following solidity code to test:
event Message(address id, bytes16 message);
function messages() public {
emit Message(address(0), "Almost there");
emit Message(address(msg.sender), "Congratulations");
}
And changed the Javascript tests to:
it("should check that the correct events are returned", async () => {
let result = await casino.messages();
truffleAssert.eventEmitted(result, 'Message', (ev) => {
return ev.id === '0x0000000000000000000000000000000000000000' && web3.utils.hexToUtf8(ev.message) === 'Almost there';
}, 'Contract should return the correct message.');
truffleAssert.eventEmitted(result, 'Message', (ev) => {
return ev.id === accounts[0] && web3.utils.hexToUtf8(ev.message) === 'Congratulations';
}, 'Contract should return the correct message.');
})
Both of these tests pass with Truffle v5, Solidity v0.5.0, truffle-assertions v0.7.1.
Best Answer
Your lucky day (had to solve this few days ago):
See that you have
truffle-core
locally in your project. If not, do:npm install truffle-core
Then use a configuration similar to this: ( Debug -> Open Configurations )
Or add a new one (simple "node" launch, then edit it)
If your code does not spawn another node process (brings trouble which are essentially an node.js bug), you should be good.