Truffle - Pet Shop - 所有测试均出错 (.sol)

发布于 2025-01-11 08:16:46 字数 6019 浏览 0 评论 0原文

我正在关注宠物店松露教程。前几个步骤有效,即我可以编译和迁移采用合约。我的软件版本是:

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;
    }
}

Migrations 文件夹: 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 测试时犯了错误,已删除,如果在获取它后错误仍然存​​在,并且答案中指出的其他错误是正确的我将单独发布。]

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

I am following the pet shop tutorial on Truffle. The first few steps work, i.e., I can compile and migrate the Adoption contract. My software versions are:

Truffle v5.4.33 (core: 5.4.33)
Ganache v7.0.1
Solidity v0.5.16 (solc-js)
Node v17.6.0
Web3.js v1.5.3

Adoption contract code:

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;
    }
}

Migrations folder:
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);
    }

Compilation and migration (after launching 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

The top line in Ganache then says Tx count of 4, like the tutorial says, and some ETH is deducted from that address.

The problems arise at testing. I have copied and pasted the tests (both .Sol and .Js) to ensure no typos but they are compiling so I don't think that's it.

But here is the TestAdoption.sol code (in the test folder)"

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;
    }
}

And the errors I get:

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)

[Edit: I made errors copying the JS test, removed, if errors persist after getting it and the other error noted in the answer right I'll post seperately.]

Could anyone point me in the right direction to get these tests working? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-18 08:16:46

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

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

但在您的测试中,您正在收养 petId 为 8 的宠物:

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

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

Your contract requires only pets with IDs between 0 and 5 can be adopted:

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

Yet on your test, you are adopting a pet with petId of 8:

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

Try adopting a pet with petId between 0 and 5 in your test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文