0

I want to know if my syntax is correct not how to catch a revert.

the task is: Call bid method from accounts[1] of Auction.sol using auctionInstance and pass itemId=0, count=6 as arguments

 it("Should NOT allow to bid more than remaining tokens", function() {
return auctionInstance.bid(itemId[0],count[6],{from:account[1]})
.then(function (result) {
  /*
  We are testing for a negative condition and hence this particular block will not have executed if our test case was correct. If this part is executed then we throw an error and catch the error to assert false
  */
  revert("Failed to check remaining tokens less than count");
}).catch(function (e) {
  var a = e.toString();
  if(e === "Failed to check remaining tokens less than count") {
  assert(false);
  } else {
    assert(true);
  }
})

The project code looks like this I had to complete it that is the code at the top.

   //Test Case for checking if the bid is more than the token amount
  it("Should NOT allow to bid more than remaining tokens", function() {
    /**********
    TASK 1:   Call bid method from accounts[1] of Auction.sol using auctionInstance and
    pass itemId=0, count=6 as arguments
    HINT:     To make a function call from account 1 use {from: accounts[1]} as an extra argument
    ***********/
    return //
    .then(function (result) {
      /*
      We are testing for a negative condition and hence this particular block will not have executed if our test case was correct. If this part is executed then we throw an error and catch the error to assert false
      */
      throw("Failed to check remaining tokens less than count");
    }).catch(function (e) {
      var a = e.toString();
      if(e === "Failed to check remaining tokens less than count") {
        /**********
        TASK 2: This is the error which we had thrown. Should you assert true or false?
        HINT:   Use assert(false) to assert false
                Use assert(true) to assert true
        ***********/
        //
      } else {
        /**********
        TASK 3: assert the opposite here
        ***********/
        //
      }
    })
  });

3
  • 1
    Help with what? There's no question here, nor any description of a problem, just a dump of some code. Commented Jan 29, 2019 at 12:50
  • 1
    The golden rule I try to remember when asking questions is "Imagine you're trying to answer the question yourself". :-) (codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question) Commented Jan 29, 2019 at 12:57
  • Ok thank you Im new to all of this. Im busy studying and this is part of a course project. the question I would have is is my syntax correct? for Task 1 below. ' TASK 1: Call bid method from accounts[1] of Auction.sol using auctionInstance and pass itemId=0, count=6 as arguments ' Commented Jan 30, 2019 at 19:20

1 Answer 1

1

The most straightforward way to test for reverts is using my truffle-assertions library. It has options to assert that a transaction fails/reverts/passes.

You can install the library with npm

npm install truffle-assertions

Then import it at the top of your file

const truffleAssert = require('truffle-assertions');

Then you can use it in your tests. In your case you could change your test to the following:

it("Should NOT allow to bid more than remaining tokens", function() {
    return truffleAssert.reverts(
        auctionInstance.bid(itemId[0], count[6], {from: account[1]}), 
        "Failed to check remaining tokens less than count"
    );
})

Or with async/await:

it("Should NOT allow to bid more than remaining tokens", async function() {
    await truffleAssert.reverts(
        auctionInstance.bid(itemId[0], count[6], {from: account[1]}), 
        "Failed to check remaining tokens less than count"
    );
})

Do note that right now you can only retrieve the revert reason string ("Failed to...") when using the latest versions of Truffle and ganache-cli. If you are using other versions, you should omit this parameter.

1
  • Thank you so much, I will give this a try there are some other questions too. But I will try this one first. Im a student and this is part of the course project. Im not sure if the marking system will allow for importing libraries but I will try. Commented Jan 30, 2019 at 19:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.