0

I wrote a function that generate a key value. the function codes is here

   mapping(string => bool) processedHash;
   mapping(uint   => bool) HashList;

function generateHash(uint256 _mintAmount, string memory _refNumber) onlyWhitelistAdmin whenNotPaused public returns(bool){
    require(processedHash[_refNumber] == false,'This transaction already processed');
    CreateKey memory cK = CreateKey({
    mintAmount:_mintAmount,
    ReferenceNumber:_refNumber});
    uint _Hash = uint(keccak256(abi.encodePacked(cK.mintAmount, cK.ReferenceNumber)));
    require(HashList[_Hash] == false,'This hash already generated.');
    HashList[_Hash] = true;
    processedHash[_refNumber] = true;
    return true;
    }

if generated a key before by a reference number, the key mustn't generate there for first require must run in the function and then the transaction must revert.

also I wrote this test in truffle:


const assertLib = require('truffle-assertions');
...

it('should not generate the hash for same reference number', async()=>{
  actions.generateHash.call(100, '123', {from: accounts[0]});
  await assertLib.reverts(actions.generateHash.call(100, '123', {from: accounts[0]}));

  });  

Truffle can't catch the require statement. And truffle giving this output to me

AssertionError: Did not fail

how can I catch this require statement? or how can I write test for the function?

thank you,

| improve this question | | | | |
1
    it('should not generate the hash for same reference number', async()=>{
        actions.generateHash.call(100, '123', {from: accounts[0]});
        await assertLib.reverts(actions.generateHash.call(100, '123', {from: 
        accounts[0]}));

     }); 

In the above test statement you have specified generatehash.call() .call is used to read state variables, due to the use of .call the first call to generateHash did not make any state changes as a result of which the second call is not reverting, try this...

    it('should not generate the hash for same reference number', async()=>{
        const receipt = await actions.generateHash(100, '123', {from: accounts[0]});
        console.log('Receipt: ', receipt); // For your reference
        await assertLib.reverts(actions.generateHash(100, '123', {from: 
        accounts[0]}));

     }); 

The assertion will pass in this case.

| improve this answer | | | | |
  • thank you! that's passed! – sollover Mar 28 at 11:13

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.