当前位置: 首页 > 知识库问答 >
问题:

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

臧亦
2023-03-14

所以我正在构建一个基本的NF令牌。创建了一个基本的铸币函数和映射。我用truffle测试了应用程序,尝试了solidity测试和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,并将其与测试中声明的预期变量(letexpected=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契约中的函数。在solidity中编写测试契约时,它几乎总是说“错误:处理事务时的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]; 
    }


}

共有1个答案

芮建茗
2023-03-14

NFCertificate.Deployment()返回承诺,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关键字发出它。用大写开头也是很好的,否则它可能看起来像一个函数。So事件ReturnNumbers(uint Numbers);

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

添加Async/Await行应该可以修复JavaScript测试。如果您以后想要断言returnNumbers事件正确发出,我建议使用我的truffle-assertions库,它包括用于断言事件是否已发出的函数。它还包括以直接的方式断言还原和其他失败的函数。

 类似资料:
  • 版本申明 pragma solidity ^0.4.0; 说明: 1 版本要高于0.4才可以编译 2 号表示高于0.5的版本则不可编译,第三位的版本号但可以变,留出来用做bug可以修复(如0.4.1的编译器有bug,可在0.4.2修复,现有合约不用改代码)。 引用其它源文件 全局引入 * import “filename”; 自定义命名空间引入 * import * as symbolN

  • 当将智能合约部署到私有区块链,并试图通过Android Studio应用程序对智能合约中的方法调用执行SendAsync时,我们会在控制台中收到一条消息,声明以下消息:I/System。出局:java。util。同时发生的CompletableFuture@427c299[未完成]。问题在于我的MainActivity中名为VarName的变量。java文件如下所示。 我试图通过创建交易收据的传统

  • 我正在尝试使用去以太坊库通过移动设备(android)与智能合约进行交互。 Android系统 合同 预期行为 在林克比区块链号上与部署的智能合同成功互动。 实际行为 对于 setter(在合同上):'abi:不能使用切片作为类型字符串作为参数' 对于 getter(在合约上):'abi:无法在 []接口 {}中取消使用字符串 复制行为的步骤 1.)通过手机连接到Rinkeby测试网 2.)通过手

  • 指导编写一个EOSIO的智能合约 模块 Account API 查询账户数据的API. Chain API 查询链内部状态的API. Database API 存储和检索EOS.IO区块链的数据API根据以下广泛结构来组织数据. Math API 定义常用的数学函数. Action API 定义用于查询操作属性的API. Memory API 定义常用的记忆功能. Console API 使应用程

  • 编程语言 使用golang作为编程语言(对部分关键字限制,以保证处理的有序性),而不是重新创造编程语言。 golang是一个简单、易用的编程语言,它有完善的帮助文档和开发工具。 它是强类型校验,编译阶段就能够校验发现很多bug。 它是模块化的,本系统能够简单屏蔽外部功能,使智能合约处在简单可预期的环境中。 已经有大量的golang开发人员,他们如果要开发智能合约,非常容易上手。 智能合约的分类 公

  • 编辑:我刚刚意识到,即使是一个带有应用程序条的简单屏幕,也会发生这种情况 错误:任务“:app:checkdebugaarmadata”的执行失败 无法解析配置“:app:debugRuntimeClasspath”的所有文件。无法解析com。谷歌。firebase:firebase firestore:22.1.2。所需人员:项目:应用程序 无法解析com。谷歌。firebase:firebas