17

当我想测试一个字符串值是否正确时,就会出现问题。数字被正确断言,并且在尝试编译时不会返回错误消息。但是,当我尝试断言一个字符串时,它会返回以下错误消息:

Error: Member "equal" is not available in type(library Assert) outside of storage.
        Assert.equal(token.symbol(), "$", "The symbol of the token should be $");
        ^----------^
Compiliation failed. See above.

令牌.sol

pragma solidity ^0.4.8;

contract Token {
    /* The amount of tokens a person will get for 1 ETH */
    uint256 public exchangeRate;

    /* The name of the token */
    string public name;

    /* The address which controls the token */
    address public owner;

    /* The symbol of the token */
    string public symbol;

    /* The balances of all registered addresses */
    mapping (address => uint256) balances;

    /* Token constructor */
    function Token(uint256 _exchangeRate, string _name, string _symbol) {
        exchangeRate = _exchangeRate;
        name = _name;
        owner = msg.sender;
        symbol = _symbol;
    }

    function getBalance(address account) returns (uint256 balance) {
        return balances[account];
    }
}

测试令牌.sol

pragma solidity ^0.4.8;

// Framework libraries
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";

// Custom libraries and contracts
import "../contracts/Token.sol";

contract TestToken {
    function testExchangeRate() {
        Token token = new Token(500, "Dollar", "$");

        uint256 expected = 500;

        Assert.equal(token.exchangeRate(), expected, "The exchange rate should be 500 tokens for 1 ETH");
    }

    function testSymbol() {
        Token token = new Token(500, "Dollar", "$");

        Assert.equal(token.symbol(), "$", "The symbol of the token should be $");
    }
}

为什么会发生,您如何解决?

4

2 回答 2

2

截至目前,solidity 不支持在合约之间返回字符串。因为在调用时字符串的长度是未知的。所以它们只支持固定大小的数组,比如 bytes32。

您可以有多个 bytes32 来存储字符串的不同部分。

于 2017-09-13T17:09:32.993 回答
-3

尝试将类型从更改string为另一种类型,例如bytes32. 有用。

一切顺利。

于 2017-06-28T14:10:53.607 回答