Solidity:TypeError:无法通过尝试测试来读取简单 HelloWorld 合约中未定义的属性(读取“JsonRpcProvider”)

问题描述 投票:0回答:5

我正在尝试测试一个简单的 HelloWorld.sol 文件:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld 
{
    function hello() public pure returns (string memory)
    {
        return "Hello, World";
    }
}

使用 HelloWorld.ts 测试文件

import "@nomiclabs/hardhat-ethers" ;
import { ethers } from "hardhat";
import { expect } from "chai";


describe("hello world", function()
{
    it("should say hello world", async function () 
    {
        const HelloWorld = await ethers.getContractFactory("HelloWorld");
        const hello = await HelloWorld.deploy();

        expect(hello).to.equal("Hello, World");
       
    });
});

调用后:npx 安全帽测试

I got result with a error message:

hello world
    1) should say hello world


  0 passing (78ms)
  1 failing

  1) hello world
       should say hello world:
     TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
      at Object.<anonymous> (node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:4:61)
      at Module._compile (node:internal/modules/cjs/loader:1218:14)
      at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
      at Object.require.extensions.<computed> [as .js] (node_modules\ts-node\src\index.ts:1608:43)
      at Module.load (node:internal/modules/cjs/loader:1081:32)
      at Function.Module._load (node:internal/modules/cjs/loader:922:12)
      at Module.require (node:internal/modules/cjs/loader:1105:19)
      at require (node:internal/modules/cjs/helpers:103:18)
      at Object.<anonymous> (node_modules\@nomiclabs\hardhat-ethers\src\internal\provider-proxy.ts:7:1)
      at Module._compile (node:internal/modules/cjs/loader:1218:14)

我已经在网上进行了答案/修复研究,但无法找到合适的..

所以我不知道如何解决它以及我应该做什么?

提前致谢!

请看上面

不知道为什么我会收到此错误...

testing typeerror solidity
5个回答
4
投票

对我来说,将

package.json
中的 ethers 版本更改为 ^5.7.2(我安装了
6.2.0
),然后删除 node_modules 文件夹并运行
yarn
再次安装软件包清除了错误。

参考自这里


1
投票

@sung 说的是正确的。但更好的是,不要在

.json
文件中手动降级以太版本,然后安装节点模块并尝试记住之前可能安装的所有软件包。只需运行
npm install ethers@^5.7.2
。运行命令后,npm 会将 ethers 包降级到版本
5.7.2
,同时保持其他包不变。版本号前面的
^
符号用于指定兼容版本的范围。在这种情况下,这意味着 npm 将安装与版本
5.x.x
兼容的
5.7.2
系列的最新版本。由于
5.7.2
是您想要的特定版本,npm 将安装该版本,因为它在指定范围内。

-干杯。


0
投票

使用

部署合约后,查看您的合约实例
const hello = await HelloWorld.deploy();

您必须调用

hello()
函数才能获得正确的输出。 更新您的代码

describe("hello world", function()
{
    it("should say hello world", async function () 
    {
        const HelloWorld = await ethers.getContractFactory("HelloWorld");
        const helloWorldContract = await HelloWorld.deploy();
        await helloWorldContract.deployed();

        const hello = await helloWorldContract.hello();
        expect(hello).to.be.equal("Hello, World");
       
    });
});

您会注意到我还添加了代码

await helloWorldContract.deployed();
,这将等到部署交易已经在链上。

祝你好运!


0
投票

此错误消息表明 JsonRpcProvider 对象未定义。此错误可能是由 @nomiclabs/hardhat-ethers 库的问题引起的。

一个可能的解决方案是将 @nomiclabs/hardhat-ethers 包更新到最新版本,因为该问题可能已在较新版本中得到修复。您可以通过在终端中运行以下命令来完成此操作:

npm install @nomiclabs/hardhat-ethers@latest

如果更新库无法解决问题,您还可以尝试检查库创建者提供的文档和示例代码,或者联系库的支持渠道寻求帮助。


0
投票

解决这个问题的方法是将我的以太版本降级到 5.7.2 即

npm i [email protected]
© www.soinside.com 2019 - 2024. All rights reserved.