testing - Solidity:类型错误:无法通过尝试测试来读取简单 HelloWorld 合约中未定义的属性(读取 'JsonRpcProvider' )

标签 testing typeerror solidity

我正在尝试测试一个简单的 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)

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

所以我不知道如何解决这个问题以及我应该做什么?

提前致谢!

请参阅上文

不知道为什么会出现此错误...

最佳答案

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

referenced from here

关于testing - Solidity:类型错误:无法通过尝试测试来读取简单 HelloWorld 合约中未定义的属性(读取 'JsonRpcProvider' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75391122/

相关文章:

python - 如何修复 "TypeError: unsupported operand type(s) for +: ' NoneType' 和 'str'“?

python - Tensorflow 类型错误 : 'module' object is not callable - tf. contrib.rnn

ethereum - 为扩展 openzeppelin ERC721URIStorage 合约的合约设置铸币价格

php - 如何测试使用字符串正文发送的 Laravel 邮件,而不是使用 Mailable

java - datajpatest + springframework 测试Sql编码特殊字符在Windows上失败

iphone - 在 iPhone 模拟器中模拟电话中断

javascript - 运行测试用例后清理智能合约存储

reactjs - 开 Jest : Cannot find module 'react' , 和其他 node_modules

javascript - 类型错误: "TypeError: function name is not a function at HTMLButtonElement.onclick (/:2:54)"

javascript - 区 block 链中如何保护源代码不被篡改?