javascript 错误:Chai属性无效:还原方式

vjrehmav  于 2022-11-20  发布在  Java
关注(0)|答案(4)|浏览(167)

我想测试我的契约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的文件

资金管理测试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!")
        })
    })
})

我做错了什么?

0ejtzxu1

0ejtzxu11#

revertedWith()实际上并不是Chai的一部分,它来自hardhat,请确保在脚本的顶部设置const {ethers} = require(“hardhat”)
例如:

const { ethers } = require("hardhat");
const { expect } = require("chai");
wwwo4jvm

wwwo4jvm2#

您只需要安装hardhat-chai-matchers如下:

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

然后将下面这一行添加到hardhat.config.js中:

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

0aydgbwb3#

您需要将ethereum-waffle添加到您的软件包中。安装了@nomiclabs/hardhat-waffle后,它无法正常工作!

yarn add --dev ethereum-waffle

完成后,您将能够使用revertedWith
最近安全帽的新包安全帽工具箱已经取代了一些基本包here

0pizxfdo

0pizxfdo4#

您不必导入revertedWith,这是导致Error: Invalid Chai property: revertedWith错误的原因。
尝试将chai导入行更改为:

const { assert, expect } = require("chai")

相关问题