繁体   English   中英

测试我的 Solidity 智能合约时遇到问题(带松露)

[英]Having a Problem Testing my Solidity Smart Contract (w/ Truffle)

所以我正在构建一个基本的 NF 令牌。 创建了一个基本的铸造功能和映射。 我用松露测试应用程序,尝试了可靠性测试和 JS 测试。 出现以下错误。

Using network 'development'.

Compiling ./contracts/NFCertificate.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/AddressUtils.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/introspection/ERC165.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/introspection/SupportsInterfaceWithLookup.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721Basic.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721BasicToken.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721Receiver.sol...
Compiling ./test/TestCertificate.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...

Compilation warnings encountered:

/Users/aditya/Desktop/Work & Hobbies/Ideas/Blockchain/Blockchain Development/Ethereum:dApp/CertificateContract/contracts/NFCertificate.sol:26:35: Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data.
        uint256 tokenId = uint256(keccak256(certificateNum, msg.sender, title, message));
                                  ^---------------------------------------------------^



  TestCertificate
    1) testNumber
    > No events were emitted

  Contract: NFCertificate
    2) It should return the same number
    > No events were emitted


  0 passing (1s)
  2 failing

  1) TestCertificate
       testNumber:
     Error: VM Exception while processing transaction: revert
      at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

  2) Contract: NFCertificate
       It should return the same number:
     TypeError: instance.returnNumbers is not a function
      at Context.<anonymous> (test/TestCertificate.js:9:31)
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/testing/testrunner.js:135:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/property.js:119:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:89:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

测试脚本在合约中运行一个简单的内部函数,该函数返回 int = 1000,并将其与测试中声明的预期变量(让 expected = 1000)进行比较。 这是JS测试脚本

  import assertRevert from "zeppelin- 
  solidity/test/helpers/assertRevert";

  const NFCertificate = artifacts.require("NFCertificate");

  contract("NFCertificate", () => {
      it("It should return the same number", function() {
          let instance = NFCertificate.deployed();
          let expected = 1000;
          assert.equal(instance.returnNumber(), expected);
      });
  });

这是 Solidity 测试脚本:

pragma solidity ^0.4.20;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/NFCertificate.sol";

contract TestCertificate {

  function testNumber() public {
    NFCertificate cert = NFCertificate(DeployedAddresses.NFCertificate());
    uint expected = 1000;
    Assert.equal(cert.returnNumber(), expected, "Numbers should be equal");
  }
}

我还尝试测试铸造的令牌 ID,以及基于我声明的映射的令牌所有者,我遇到了同样的问题。 在用 javascript 编写测试合约时,它无法识别原始 NFT 合约中的功能。 在可靠地编写测试合约时,它几乎总是说“错误:处理事务时出现 VM 异常:还原”,没有别的。

最后,这是我试图测试的合同。 感谢任何和所有帮助,我对编码和以太坊非常陌生,所以我可能犯了很多错误。

pragma solidity ^0.4.24;

import '../node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721BasicToken.sol';
import '../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol';

contract NFCertificate is ERC721BasicToken, Ownable {
    struct Certificate {
        uint certNum;
        uint256 tokenId;
        bytes32 title;
        bytes32 message;
        address owner;
    }

    mapping (uint256 => address) tokenToOwner;
    mapping (address => uint256) ownerToToken;
    mapping (uint256 => string) tokenIdToName;

    event returnNumbers(uint number);

    Certificate[] public certificates;

    function createCert(bytes32 title, bytes32 message) public returns (bytes32){
        uint certificateNum = certificates.length - 1; 
        uint256 tokenId = uint256(keccak256(certificateNum, msg.sender, title, message));
        certificates.push(Certificate(certificateNum++, tokenId, title, message, msg.sender));
        tokenToOwner[tokenId] = msg.sender;
        ownerToToken[msg.sender] = tokenId;
        _mint(msg.sender, tokenId);
    }

    function returnNumber() public returns(uint) {
        uint number = 1000;
        returnNumbers(number);
        return number;
    }

    function whatTokensDoYouOwn(address owner) public view returns(uint256) {
        return ownerToToken[owner]; 
    }


}

NFCertificate.deployed()返回一个 promise, instance.returnNumber() 所以JS应该是:

contract("NFCertificate", () => {
  it("It should return the same number", async function() {
      let instance = await NFCertificate.deployed();
      let expected = 1000;
      assert.equal(await instance.returnNumber(), expected);
  });
});

由于returnNumbers是一个事件,它应该使用emit关键字emit 以大写开头的事件样式也很好,否则它可能看起来像一个函数。 所以event ReturnNumbers(uint number);

function returnNumber() public returns(uint) {
    uint number = 1000;
    emit ReturnNumbers(number);
    return number;
}

添加 async/await 行应该可以修复您的 JavaScript 测试。 如果您稍后想断言您的ReturnNumbers事件已正确发出,我建议使用我的truffle-assertions库,其中包含断言事件已发出或尚未发出的函数。 它还包括以直接方式断言恢复和其他故障的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM