错误:无效的 Chai 属性:revertedWith

编程入门 行业动态 更新时间:2024-10-06 06:51:49

错误:无效的 Chai <a href=https://www.elefans.com/category/jswz/34/1771415.html style=属性:revertedWith"/>

错误:无效的 Chai 属性:revertedWith

我想测试我的合约FundMe.sol但是它是如何起飞的FundMe.test.js弹出一个错误

我的控制台

FundMe
    constructor
Development network detected! Deploying mocks...
      ✓ Should set the aggregator addresses correctly
    fund

      1) Should fail if you don't send enough ETH


  1 passing (977ms)
  1 failing

  1) FundMe
       fund
         Should fail if you don't send enough ETH:
     Error: Invalid Chai property: revertedWith
      at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:78:17)
      at Context.<anonymous> (test/unit/FundMe.test.js:29:46)

测试写入文件的文件FundMe.sol

FundMe.test.js

const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect, revertedWith } = require("chai")

describe("FundMe", async function() {
    let fundMe
    let deployer
    let mockV3Aggregator
    beforeEach(async function() {
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"]) // deploy all contracts using deployment.fixture()
        fundMe = await ethers.getContract("FundMe", deployer)
        mockV3Aggregator = await ethers.getContract(
            "MockV3Aggregator",
            deployer
        )
    })

    describe("constructor", async function() {
        it("Should set the aggregator addresses correctly", async function() {
            const response = await fundMe.priceFeed()
            assert.equal(response, mockV3Aggregator.address)
        })
    })

    describe("fund", async function() {
        it("Should fail if you don't send enough ETH", async function() {
            await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!")
        })
    })
})

我做错了什么?

回答如下:

你只需要像这样安装hardhat-chai-matchers

$ yarn add --dev @nomicfoundation/hardhat-chai-matchers

npm install --save-dev @nomicfoundation/hardhat-chai-matchers

然后将此行添加到您的hardhat.config.js

require("@nomicfoundation/hardhat-chai-matchers");

更多信息:https://hardhat/hardhat-chai-matchers/docs/overview

更多推荐

错误:无效的 Chai 属性:revertedWith

本文发布于:2024-05-13 14:51:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   错误   Chai   revertedWith

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!