我正在用松露开发虚拟测试合同,代码如下,
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/SkillDevelopment.sol";
contract TestSkillDevelopment {
SkillDevelopment skillDevelopmentContract = SkillDevelopment(DeployedAddresses.SkillDevelopment());
function testSetStudentEnrollInfo() public {
skillDevelopmentContract.setStudentEnrollInfo("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
string expected = string("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
Assert.equal(skillDevelopmentContract.getStudentEnrollInfo(), expected, "The message should be set");
}
}
但是收到错误
“TypeError:不允许从”literal_string“到字符串存储指针”的显式类型转换。“
在运行“truffle test”命令时。
请提示,这里有多么不正确。
答案 0 :(得分:1)
请改为尝试:
string memory expected = "{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}";
string
的默认位置是storage
,这意味着您必须将其指向存储中的某个状态变量。切换到memory
可解决该问题。最后,显式转换为string
是不必要的(因为该值已经是一个字符串)并导致您看到的编译错误。