错误:无效的 Chai 属性:revertedWith

Paw*_*ach 4 javascript node.js blockchain hardhat

我想测试我的合约FundMe.sol但它如何脱离FundMe.test.js会弹出一个错误

\n

我的控制台

\n
FundMe\n    constructor\nDevelopment network detected! Deploying mocks...\n      \xe2\x9c\x93 Should set the aggregator addresses correctly\n    fund\n\n      1) Should fail if you don't send enough ETH\n\n\n  1 passing (977ms)\n  1 failing\n\n  1) FundMe\n       fund\n         Should fail if you don't send enough ETH:\n     Error: Invalid Chai property: revertedWith\n      at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:78:17)\n      at Context.<anonymous> (test/unit/FundMe.test.js:29:46)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

FundMe.test.js

\n
const { deployments, ethers, getNamedAccounts } = require("hardhat")\nconst { assert, expect, revertedWith } = require("chai")\n\ndescribe("FundMe", async function() {\n    let fundMe\n    let deployer\n    let mockV3Aggregator\n    beforeEach(async function() {\n        deployer = (await getNamedAccounts()).deployer\n        await deployments.fixture(["all"]) // deploy all contracts using deployment.fixture()\n        fundMe = await ethers.getContract("FundMe", deployer)\n        mockV3Aggregator = await ethers.getContract(\n            "MockV3Aggregator",\n            deployer\n        )\n    })\n\n    describe("constructor", async function() {\n        it("Should set the aggregator addresses correctly", async function() {\n            const response = await fundMe.priceFeed()\n            assert.equal(response, mockV3Aggregator.address)\n        })\n    })\n\n    describe("fund", async function() {\n        it("Should fail if you don't send enough ETH", async function() {\n            await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!")\n        })\n    })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

我究竟做错了什么?

\n

小智 5

你只需要像这样安装hardhat-c​​hai-matchers :

$ yarn add --dev @nomicfoundation/hardhat-chai-matchers
Run Code Online (Sandbox Code Playgroud)

或者

npm install --save-dev @nomicfoundation/hardhat-chai-matchers
Run Code Online (Sandbox Code Playgroud)

然后将此行添加到您的Hardhat.config.js中:

require("@nomicfoundation/hardhat-chai-matchers");
Run Code Online (Sandbox Code Playgroud)

更多信息:https://hardhat.org/hardhat-c ​​hai-matchers/docs/overview