PatrickAlphaC / hardhat-fund-me-fcc

82 stars 183 forks source link

Error: sending a transaction requires a signer (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=contracts/5.7.0) while testing Fund Me withdraw function #137

Open rbansode opened 1 year ago

rbansode commented 1 year ago

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();

        const attacker = accounts[1];

        const attackerConnectedContract = await fundMe.connect(attacker);

        await expect(attackerConnectedContract.withdraw()).to.be.reverted;
    });

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

franciscocfreire commented 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?

hendythee commented 1 year ago

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;
rbansode commented 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?

Yes. I was missing the 'await' before ethers.getSigners();

haris4121 commented 1 year ago

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?

egutierrez130 commented 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 was having the same issue but adding await , resolved this. Thanks @franciscocfreire !

Sammycoderr commented 1 year ago

but how come his own worked without the await

Sammycoderr commented 1 year ago

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

Yehezkiel-simbuang commented 1 year ago

@Sammycoderr try to use

to.be.revertedWithCustomError(Contract, 'FundMe_NotOwner')

elvisisvan commented 7 months ago

.