Search code examples
unit-testingmappingsolidity

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


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()
    )
})

Solution

  • 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()
        )
    })