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)