Solidity

Ethers.js 測試:給安全帽生成的賬戶 ETH

  • January 23, 2022
   Deployment
     √ Should set the contract's address as owner (44ms)
     √ Should assign the totalSupply of credits to the creditsContract (114ms)
   Transactions
     1) Should fail if sender doesn't have enough tokens
     2) Should fail when user tries to access contract approve without access


 2 passing (5s)
 2 failing

 1) Credits.sol: Uint Tests
      Transactions
        Should fail if sender doesn't have enough tokens:
    AssertionError: Expected transaction to be reverted with Not enough tokens, but other exception was thrown: Error: insufficient funds for intrinsic transaction cost (error="Error: VM Exception while processing transaction: revert insufficient funds, revert", method="sendTransaction", transaction=undefined, code=INSUFFICIENT_FUNDS, version=providers/5.0.17)


 2) Credits.sol: Uint Tests
      Transactions
        Should fail when user tries to access contract approve without access:
    AssertionError: Expected transaction to be reverted with No access, but other exception was thrown: Error: invalid ENS name (argument="name", value="<SignerWithAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>", code=INVALID_ARGUMENT, version=providers/5.0.17)

錯誤出現在以下程式碼中,但是我不知道如何將 eth 添加到帳戶中:/ 有什麼建議嗎?

const { expect } = require("chai");

describe("Credits.sol: Uint Tests", () => {
   let Credits, credits, addr1, addr2, addr3, addr4;

   beforeEach(async () => {
       Credits = await ethers.getContractFactory("Credits");
       credits = await Credits.deploy();
       [addr1, addr2, addr3, addr4, _] = await ethers.getSigners();
   });

   describe("Deployment", () => {
       it("Should set the contract's address as owner", async () => {
           expect(await credits.creditsContract()).to.equal(credits.address);
       });

       it("Should assign the totalSupply of credits to the creditsContract", async () => {
           let creditsContractBal = await credits.balanceOf(credits.address);
           expect(await credits.remainingUnheldCredits()).to.equal(creditsContractBal);
       });
   });

   describe("Transactions", () => {
       // it("Should use transfer() to send tokens from addr1 to addr2", async () => {
       //     credits.contractApprove(credits.address, addr1, 100);
       //     await credits.connect(addr1).transfer(addr2.address, 50);
       //     let addr2Balance = await credits.balanceOf(addr2.address);
       //     expect(addr2Balance).to.equal(50);
       //     expect(credits.balanceOf(addr1.address)).to.equal(0);
       // });

       // it("Should transfer tokens from addr1 to addr2", async () => {
       //     // Transfer from addr1 to addr2
       //     await token.connect(addr1).transfer(addr2.address, 50);
       //     let addr2Balance = await token.balanceOf(addr2.address);
       //     expect(addr2Balance).to.equal(50);
       //     addr1Balance = await token.balanceOf(addr1.address);
       //     expect(addr1Balance).to.equal(0);
       // });

       it("Should fail if sender doesn't have enough tokens", async () => {
           const initalCreditsSupply = await credits.balanceOf(credits.address);

           await expect(credits.connect(addr1).transfer(credits.address, 1)).to.be.revertedWith("Not enough tokens");
           expect(await credits.balanceOf(credits.address)).to.equal(initalCreditsSupply);
       });


       it("Should fail when user tries to access contract approve without access", async () => {
           await expect(credits.connect(addr1).contractApprove(credits.address, addr1, 100)).to.be.revertedWith("No access");
           expect(await credits.users[credits.address].allowance[addr1]).to.equal(0);
       });
       // it("Should update balances after transfers", async () => {
       //     const initialOwnerBalance = await token.balanceOf(owner.address);

       //     await token.transfer(addr1.address, 100);
       //     await token.transfer(addr2.address, 50);

       //     const finalOwnerBalance = await token.balanceOf(owner.address);
       //     expect(finalOwnerBalance).to.equal(initialOwnerBalance - 150);

       //     const addr1Balance = await token.balanceOf(addr1.address);
       //     expect(addr1Balance).to.equal(100);

       //     const addr2Balance = await token.balanceOf(addr2.address);
       //     expect(addr2Balance).to.equal(50);
       // });
   });

   // describe("", () => {
   //     it("Should fail if sender doesn't have enough tokens", async () => {
   //         const initialOwnerBalance = await token.balanceOf(owner.address);

   //         await expect(token.connect(addr1).transfer(owner.address, 1)).to.be.revertedWith("Not enough tokens");
   //         expect(await token.balanceOf(owner.address)).to.equal(initialOwnerBalance);
   //     });
   // });
}); ```

這發生在我身上,我不知道如何解決它。我正在使用乙太幣測試一家銀行

console.log("  ===== Bank---> admin.getBalance(): " + (await adminSigner.getBalance()));

輸出如下

===== Bank---> admin.getBalance(): 9999979959057829397771

然而消息錯誤表明管理員沒有任何乙太幣:

Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance'

如果將 to.be.revertedWith 替換為 to.be.reverted 應該可以工作

引用自:https://ethereum.stackexchange.com/questions/92365