我正在为我的智能合同坚实版0.4.24编写松露测试。这些测试正在释放事件,以便通过智能契约将数据写入内存。但我无法从函数中检索数据。
以下是我的智能合同功能:
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;
}下面是松露测试:
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]);
}控制台日志:
Farm ID: 0
Farm Name:
Farm Latitude:
Farm Longitude:
Farm Address:测试期间发出的事件:
ProducerAdded(account: <indexed>)
FarmRegistered(farmId: 1)
GrapesHarvested(grapesId: 1)发布于 2019-03-11 14:47:46
这部分做不到你想做的。
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);
}首先,将函数参数分配给memory变量newLocation。然后,将参数分配给另一个memory变量newFarm。他们都是memory,所以他们不会坚持。
考虑:
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是一个存储指针,所以分配给它直接到存储。函数执行完毕后,存储将保持不变。当memory变量的函数或函数作用域(新编译器进一步缩小作用域)结束时,它们将被熄灭。
在进入单元测试之前,您可能会发现Remix在进行表面测试和手动测试时非常方便。
希望能帮上忙。
https://ethereum.stackexchange.com/questions/68219
复制相似问题