Unit Testing in Truffle

From the video: Creating wallet test.
When I do:

contract("Dex", accounts => {
    it("Should only be possible for owner to add tokens", async ()=>{
        let dex = await Dex.deployed();
        let link = await Link.deployed();
        
        await truffleAssert.passes(
            await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]} )
        );  

        await truffleAssert.reverts(
            await dex.addToken(web3.utils.fromUtf8("AAVE"), link.address, {from: accounts[1]} )
        ); 
    } )

I don’t get a pass. I get:


  Contract: Dex
    1) Should only be possible for owner to add tokens
    > No events were emitted


  0 passing (6s)
  1 failing

  1) Contract: Dex
       Should only be possible for owner to add tokens:
     Error: Returned error: VM Exception while processing transaction: revert Ownable: caller is not the owner -- Reason given: Ownable: caller is not the owner.
      at Context.<anonymous> (C:\Users\rapha\Documents\NFT\ETH201\DEX\test\wallettest.js:15:23)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

The error message is correct. But I wanted it to revert.
Why does it behave differently than @filip’s code?
Also I get 0 passed. But the first test should have passed…

I figured it out, each test should start with it("…") again.
Works like a charm.

Alternative not using truffle assertions but native JS is try catch.
Like here:
https://ethereum.stackexchange.com/questions/48627/how-to-catch-revert-error-in-truffle-test-javascript/48629

2 Likes

Please how do I code a 40% return to an investor after 30 days everytime he deposit in to the contract using solidity
I have used block.timestamp but I want to put it all together how do I do it?

please whenever i deploy this contract using remix
it deploy successfully
whenever i call the sendeth function in the contract eth is not being sent to the address
and when i call the get balance function the balance still show that the eth recieved is still there please what should i do that when ever i call the sendeth function it actually send eth and the address i send the eth to recieves it
HERE IS THE CONTRACT:
pragma solidity 0.5.12;

contract Transfer{
address public owner = msg.sender;

 function sendeth(address payable _to) public payable {
  _to.transfer(msg.value);   
  require(owner == msg.sender);
 }
  
  function recieve() public payable{
   require(msg.value > 0); 
   
  }

 function getBalance() view public  returns (uint){
  return address(this).balance; 
 }

}`

Preformatted text

`

Blockquote

Actually the problem with the code here is that you are awaiting the dex.addToken() call inside the truffleAssert.reverts() function. truffleAssert.revert() is actually expecting a promise that it can resolve and deal with the outcome. But when you use await inside of it. It actually resolves to the error that is returned by the dex.addToken() now allowing the truffleAssertions to handle it, so the execution breaks even before truffleAssertions can do its thing. the correct usage is given below…

await truffleAssert.reverts(dex.addToken(web3.utils.fromUtf8("AAVE"), link.address, {from: accounts[1]} )); 

So you don’t need the implementation mentioned in the stackexchange question.

1 Like