0

I am writing truffle tests for my smart contract solidity version 0.4.24. The tests are emitting events to write data to memory through smart contract. But I am unable to retrieve data from functions.

Here's my smart contract functions:

previousFarmId = 0;

struct Location {
        string latitude;
        string longitude;
        string locationAddress;
    }

    struct Farm {
        uint farmId;
        string farmName;
        Location location;
    }

mapping (uint => Farm) farms;

function registerFarm(string _farmName, string _farmLatitude, string _farmLongitude, string _locationAddress) public {

        previousFarmId = previousFarmId + 1;

        Location memory newLocation = farmLocation[previousFarmId];
        newLocation.latitude = _farmLatitude;
        newLocation.longitude = _farmLongitude;
        newLocation.locationAddress = _locationAddress;

        Farm memory newFarm = farms[previousFarmId];
        newFarm.farmId = previousFarmId;
        newFarm.farmName = _farmName;
        newFarm.location = newLocation;

        emit FarmRegistered(previousFarmId);

    }

    function getFarmInfo(uint _farmId) public view 
    returns (uint farmId, string farmName, string latitude, string longitude, string locationAddress) {
        Farm memory returnFarm = farms[_farmId];
        farmId = returnFarm.farmId;
        farmName = returnFarm.farmName;
        latitude = returnFarm.location.latitude;
        longitude = returnFarm.location.longitude;
        locationAddress = returnFarm.location.locationAddress;
    }

Here is the truffle test:

const farmID = 1;

it("Testing smart contract function harvestGrapes() that allows producer to harvest grapes", async() => {

        const supplyChain = await SupplyChain.deployed();

        await supplyChain.addProducer(producerID, {from: deployerID});

        await supplyChain.registerFarm(farmName, farmLatitude, farmLongitude, farmAddress, {from: producerID});

        const resultFarms = await supplyChain.getFarmInfo.call(farmID);

        console.log('Farm ID: ' + resultFarms[0]);
        console.log('Farm Name: ' + resultFarms[1]);
        console.log('Farm Latitude: ' + resultFarms[2]);
        console.log('Farm Longitude: ' + resultFarms[3]);
        console.log('Farm Address: ' + resultFarms[4]); 
}

Console Logs:

Farm ID: 0
Farm Name:
Farm Latitude:
Farm Longitude:
Farm Address:

Events emitted during test:

   ProducerAdded(account: <indexed>)
    FarmRegistered(farmId: 1)
    GrapesHarvested(grapesId: 1)
1

This part doesn't do what you want.

function registerFarm(string _farmName, string _farmLatitude, string _farmLongitude, string _locationAddress) public {

        previousFarmId = previousFarmId + 1;

        Location memory newLocation = farmLocation[previousFarmId];
        newLocation.latitude = _farmLatitude;
        newLocation.longitude = _farmLongitude;
        newLocation.locationAddress = _locationAddress;

        Farm memory newFarm = farms[previousFarmId];
        newFarm.farmId = previousFarmId;
        newFarm.farmName = _farmName;
        newFarm.location = newLocation;

        emit FarmRegistered(previousFarmId);

    }

First you assign function arguments to the memory variable newLocation. Then, you assign the arguments to another memory variable newFarm. They're both memory, so they won't persist.

Consider:

function registerFarm(string _farmName, string _farmLatitude, string _farmLongitude, string _locationAddress) public {

        previousFarmId = previousFarmId + 1;

        Location storage newLocation = farmLocation[previousFarmId];
        newLocation.latitude = _farmLatitude;
        newLocation.longitude = _farmLongitude;
        newLocation.locationAddress = _locationAddress;

        emit FarmRegistered(previousFarmId);

    }

newLocation is a storage pointer, so assignments to it go directly to storage. Storage will persist after the function finishes executing. memory variables are extinguished when their function or in-function scope (new compiler further narrows scopes) finishes.

You might find Remix is handy for superficial and manual testing before moving on to unit tests.

Hope it helps.

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.