Solidity
小號OLV_和D小號○大號在和DSOLVEDPatrickCollins Solidity Course 2022 - 第 7 課:11:20:38 -> TypeError:ethers.getContract 不是函式
AssertionError: Expected transaction to be reverted with You need to spend more ETH!, but other exception was thrown: Error: Transaction reverted: function returned an unexpected amount of data
在影片的 11:20:38 左右執行 fundMe() 測試時出現此錯誤
describe("FundMe", async function () { let fundMe; let deployer; let MockV3Aggregator; beforeEach(async function () { deployer = (await getNamedAccounts()).deployer; await deployments.fixture(["all"]); fundMe = await ethers.getContract("FundMe", deployer); MockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer); }); describe("constructor", async function () { it("sets the aggregator addresses correctly", async function () { const response = await fundMe.priceFeed(); assert.equal(response, MockV3Aggregator.address); }); }); describe("fund", async function () { it("Fails if you don't send enough ETH", async function () { await expect(fundMe.fund()).to.be.revertedWith( "You need to spend more ETH!" ); }); }); });
經過一些研究,似乎可以通過從安全帽文件下載@nomicslabs/hardhat-waffle 來解決這個問題
$$ https://hardhat.org/plugins/nomiclabs-hardhat-waffle $$ 但是在執行安裝後,我現在在執行上述測試時遇到以下錯誤
1) FundMe "before each" hook for "sets the aggregator addresses correctly": TypeError: ethers.getContract is not a function
關於這裡可能是什麼問題的任何想法?謝謝你!
$$ SOLVED $$ 我試圖修改該行,使其顯示為:
await expect(fundMe.fund()).to.be.reverted
正如您在下面看到的那樣,這兩個測試似乎都通過了
所以這絕對是行,但我仍然不明白為什麼添加字元串會導致錯誤,也許我缺少參數或導入。
yarn add hardhat--dev @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers
再次執行並解決了問題。
現在測試 1 通過,但測試在到達時返回以下錯誤
await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!");
錯誤:
1) FundMe fund Fails if you don't send enough ETH: AssertionError: Expected transaction to be reverted with You need to spend more ETH!, but other exception was thrown: Error: Transaction reverted: function returned an unexpected amount of data