Open rbansode opened 1 year ago
Your test code works.
maybe you forget "await" one line before
const accounts = await ethers.getSigners()
or Did you check your FundMe.sol?
I had the same issue, and following the suggestion by @franciscocfreire fixed the problem.
The other thing that would work for me is to omit 'await' before expect:
expect(attackerConnectedContract.withdraw()).to.be.reverted;
Your test code works. maybe you forget "await" one line before
const accounts = await ethers.getSigners()
or Did you check your FundMe.sol?
Yes. I was missing the 'await' before ethers.getSigners();
if i try to revert it with error
it("only allows owner to withdraw", async () => {
const accounts = await ethers.getSigners()
const fundMeConnectedContract = await fundMe.connect(accounts[1])
await expect(
fundMeConnectedContract.withdraw()
).to.be.revertedWith("FundMe__NotOwner")
})
it gives me error as
only allows owner to withdraw:
AssertionError: Expected transaction to be reverted with reason 'FundMe__NotOwner', but it reverted with a custom error
what should i do?
Your test code works. maybe you forget "await" one line before
const accounts = await ethers.getSigners()
or Did you check your FundMe.sol?
I was having the same issue but adding await , resolved this. Thanks @franciscocfreire !
but how come his own worked without the await
if i try to revert it with error
it("only allows owner to withdraw", async () => { const accounts = await ethers.getSigners() const fundMeConnectedContract = await fundMe.connect(accounts[1]) await expect( fundMeConnectedContract.withdraw() ).to.be.revertedWith("FundMe__NotOwner") })
it gives me error as
only allows owner to withdraw: AssertionError: Expected transaction to be reverted with reason 'FundMe__NotOwner', but it reverted with a custom error
what should i do?
can someone please reply to this? thank you
@Sammycoderr try to use
to.be.revertedWithCustomError(Contract, 'FundMe_NotOwner')
.
I have written the following unit test code as suggested in the video.
//Test only if the owner is able to call the withdraw function it("Only allows the owner to withdraw", async function () { //Get all the signers of the contract const accounts = ethers.getSigners();
But when I execute the code, the get the following error message: Error: sending a transaction requires a signer (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=contracts/5.7.0)
For some reason all the code that include await expect(xxxx).to.be.reverted is giving me some or other error. Please help