Truffle - Pet Shop - 所有测试的错误 (.sol)

Truffle - Pet Shop - Errors on All Tests (.sol)

我在关注宠物店tutorial on Truffle。前几个步骤有效,即我可以编译和迁移 Adoption 合约。我的软件版本是:

Truffle v5.4.33(核心:5.4.33) 伽纳彻 v7.0.1 Solidity v0.5.16 (solc-js) 节点 v17.6.0 Web3.js v1.5.3

采用合约代码:

pragma solidity ^0.5.0;

contract Adoption {
    address[16] public adopters;

    function adopt(uint petId) public returns (uint) {

        require(petId >= 0 && petId <= 5);

        adopters[petId] = msg.sender;

        return petId;    
    }

    function getAdopters() public view returns (address[16] memory) {
        return adopters;
    }
}

迁移文件夹: 1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

2_deploy_contracts.js

var Adoption = artifacts.require("Adoption");

    module.exports = function(deployer) {
        deployer.deploy(Adoption);
    }

编译迁移(启动Ganache 2.5.4后):

x@x:~/Code/Blockchain/Truffle/pet-shop-tutorial$ truffle compile

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

x@x:~/Code/Blockchain/Truffle/pet-shop-tutorial$ truffle migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   ⠋ Blocks: 0            Seconds: 0   > transaction hash:    0x32545eb8f57d03e0a90b9fc14e7e193c936632be84401a7ecc1481294c766ef4
   > Blocks: 0            Seconds: 0
   > contract address:    0xC7801C9570FD9Dfb9dFca6f29461D2Dd3e8b813e
   > block number:        1
   > block timestamp:     1646245076
   > account:             0xdFd735b7b2a4Cf28Bb7A85E2366FC73522573f8e
   > balance:             99.99616114
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00383886 ETH


2_deploy_contracts.js
=====================

   Replacing 'Adoption'
   --------------------
   ⠋ Blocks: 0            Seconds: 0   > transaction hash:    0x17f512c4fdabc0b743b2b552352059275c64969f728ae23874d3fc162c9c4a27
   > Blocks: 0            Seconds: 0
   > contract address:    0x9E552555C766a1E1FdB5823FC3F0301C04C79651
   > block number:        3
   > block timestamp:     1646245078
   > account:             0xdFd735b7b2a4Cf28Bb7A85E2366FC73522573f8e
   > balance:             99.99123784
   > gas used:            203827 (0x31c33)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00407654 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00407654 ETH

Summary
=======
> Total deployments:   2
> Final cost:          0.0079154 ETH

然后 Ganache 的第一行显示 Tx 计数为 4,就像教程所说的那样,并且从该地址中扣除了一些 ETH。

问题出现在测试中。我已经复制并粘贴了测试(.Sol 和 .Js)以确保没有拼写错误,但它们正在编译,所以我认为不是这样。

但这里是 TestAdoption.sol 代码(在测试文件夹中)

pragma solidity ^0.5.0;

contract Adoption {
    address[16] public adopters;

    function adopt(uint petId) public returns (uint) {

        require(petId >= 0 && petId <= 5);

        adopters[petId] = msg.sender;

        return petId;    
    }

    function getAdopters() public view returns (address[16] memory) {
        return adopters;
    }
}

我得到的错误:

Using network 'development'.


Compiling your contracts...
===========================
> Compiling ./test/TestAdoption.sol
> Artifacts written to /tmp/test--7680-8QYwuQbGOkD1
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



  TestAdoption
    1) testUserCanAdoptPet
    > No events were emitted
    2) testGetAdopterAddressByPetId
    > No events were emitted
    3) testGetAdopterAddressByPetIdInArray
    > No events were emitted


  0 passing (37s)
  3 failing

  1) TestAdoption
       testUserCanAdoptPet:
     Error: Returned error: VM Exception while processing transaction: revert
      at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

  2) TestAdoption
       testGetAdopterAddressByPetId:
     Error: Owner of the expected pet should be this contract
      at checkResultForFailure (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:65:1)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)

  3) TestAdoption
       testGetAdopterAddressByPetIdInArray:
     Error: Owner of the expected pet should be this contract
      at checkResultForFailure (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:65:1)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Context.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:91:1)

[编辑:我在复制 JS 测试时犯了错误,已删除,如果在获取它后错误仍然存​​在,并且正确答案中指出的其他错误我将单独 post。]

任何人都可以指出正确的方向来让这些测试工作吗?谢谢。

您的合同要求只能收养ID在0到5之间的宠物:

require(petId >= 0 && petId <= 5);

然而在你的测试中,你收养了一只 petId 为 8 的宠物:

await adoption.adopt(8, { from: accounts[0] });

尝试在测试中收养 petId 介于 0 和 5 之间的宠物。