Home > Mobile >  How do I unit test a function that returns multiples values from a set of mappings?
How do I unit test a function that returns multiples values from a set of mappings?

Time:09-06

The getData function fetch the stored data:

function getData(string memory _idNumber)
    public
    view
    returns (
        string memory,
        string memory,
        uint256
    )
{
    return (
        idNumberToName[_idNumber],
        idNumberToEmail[_idNumber],
        idNumberToAmount[_idNumber]
    );
}

My goal is to test the getData function to verify the mappings have no data, no luck so far. This is my idea how to test the getData function:

it("The mappings should be empty", async function () {
    const idNumber = ""

    const currentIdNumberToName = await dataStorage.getData(idNumber)
    const currentIdNumberToEmail = await dataStorage.getData(idNumber)
    const currentIdNumberToAmount = await dataStorage.getData(idNumber)

    const expectedIdNumberToName = ""
    const expectedIdNumberToEmail = ""
    const expectedIdNumberToAmount = "0"

    assert.equal(expectedIdNumberToName, currentIdNumberToName)
    assert.equal(expectedIdNumberToEmail, currentIdNumberToEmail)
    assert.equal(
        expectedIdNumberToAmount,
        currentIdNumberToAmount.toString()
    )
})

CodePudding user response:

It returns three parameters, so try it like

it("The mappings should be empty", async function () {
    const idNumber = ""

    const currentIdNumberToName = (await dataStorage.getData(idNumber))[0]
    const currentIdNumberToEmail = (await dataStorage.getData(idNumber))[1]
    const currentIdNumberToAmount = (await dataStorage.getData(idNumber))[2]

 //  or
   const [currentIdNumberToName,currentIdNumberToEmail,currentIdNumberToAmount] = 
      await dataStorage.getData(idNumber)
    

    const expectedIdNumberToName = ""
    const expectedIdNumberToEmail = ""
    const expectedIdNumberToAmount = "0"

    assert.equal(expectedIdNumberToName, currentIdNumberToName)
    assert.equal(expectedIdNumberToEmail, currentIdNumberToEmail)
    assert.equal(
        expectedIdNumberToAmount,
        currentIdNumberToAmount.toString()
    )
})
  • Related