4

Im using Truffle to test my smart contracts written in Solidity and I would like to mark test as passed if function call/execution fails.

Example:

await instance.cancelOrder({from: accounts[1]});

Normaly this would pass if function executes properly. This one requires certain variable to be true which it isn't and should fail. How do I do something like that?

1

You can call that certain variable right after this and check whether it is true or false according to your need. Let say the above call update the variable orderCanceled in the smart contract to true.

Right after this call you can also make an another call to get the value of orderCanceled and check whether it is as per desired or not (let say true). See the below example.

await instance.cancelOrder({from: accounts[1]});
let orderCanceled = await instance.methods.orderCanceled().call();
assert.equal(true, orderCanceled);

Hope it gives you some idea.

1

Have you checked out the Truffle Assertions package? The method truffleAssert.fails asserts "that the passed async contract function fails with a certain ErrorType and reason." There's also truffleAssert.reverts for the specific case of a revert failure. Here's an example from the docs:

await truffleAssert.reverts(
  contractInstance.methodThatShouldRevert(),
  "only owner"
);

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.