1

I wrote a lottery smart contract and gonna to test an "enter" functionality of the smart contract. enter image description here

The below code is javascript test code for the "enter" function of the smart contract.

enter image description here

When I call "enter" function with less than 0.01 ether, how can I continue the test code? (//??? part below contract.enter(...) line) In the enter function of lottery smart contract, the execution will be restricted if the msg.value is less than 0.01 ether. How can I detect it in the test code?

3
  • You can't the function has a require that will cause a revert and skip to the catch block, you either use the correct amount or change the test.
    – Julissa DC
    Jun 6, 2022 at 16:19
  • Could you let me know more exactly about what you said? Jun 8, 2022 at 12:19
  • if the answer helped you please upvote and accept
    – sola24
    Jul 5, 2022 at 8:07

1 Answer 1

0

mercifully, you don't need the try catch blocks if you're expecting it to throw an error

chai test syntax

await expect(myContract.callFunc()).to.be.revertedWith("my expected error message")

or if you don't want to check the message

await expect(myContract.callFunc()).to.be.reverted

or

await expect(myContract.callFunc()).to.throw

and for test that return a value you might use something like

    // expecting number 2
    expect(await myContract.getNumber()).to.equal(2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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