Solidity

在使用 Hardhat 對 Goerli 進行分段測試期間無法設置狀態變數

  • September 23, 2022

我可以在本地安全帽節點上的單元測試期間設置變數,但是在 Goerli 上相同的測試失敗。

這是我的測試程式碼:

const { getNamedAccounts, deployments, ethers, network } = require("hardhat")
const { developmentChains, networkConfig } = require("../../helper-hardhat-config")
const { assert, expect } = require("chai")
const fs = require("fs")
const path = require("path")

developmentChains.includes(network.name)
   ? describe.skip
   : describe("Whoopy Staging Tests", function () {
         let whoopy,
             deployer,
             player,
             cloneAddress,
             clonedContract,
             txReceipt,
             tx,
             accountConnectedClone,
             manager,
             create

         const chainId = network.config.chainId

         beforeEach(async function () {

           accounts = await ethers.getSigners()
           deployer = accounts[0]
           player = accounts[1]
           await deployments.fixture(["all"])

           const dir = path.resolve(
               __dirname,
               "/Users/boss/hardhat-smartcontract-whoopy/artifacts/contracts/Whoopy.sol/Whoopy.json"
           )

           const file = fs.readFileSync(dir, "utf8")
           const json = JSON.parse(file)
           const abi = json.abi

           whoopy = await ethers.getContract("Whoopy", deployer)
           manager = await ethers.getContract("VRFv2SubscriptionManager", deployer)
           wf = await ethers.getContract("WhoopyFactory", deployer)

           tx = await (await wf.connect(player).createClone(player.address))
           txReceipt = await tx.wait(1)


           cloneAddress = await txReceipt.events[1].args._instance
           clonedContract = new ethers.Contract(cloneAddress, abi, player)
           accountConnectedClone = clonedContract.connect(player)
       })

       describe("Whoopy Tests", function () {
           beforeEach(async function () {
               create = await accountConnectedClone.createWhoopy(
                   "Test",
                   1,
               {
                   value: ethers.utils.parseUnits("10000000000000000", "wei"),
                   gasLimit: 3000000
               })
               console.log(create)
           })
           it("creates Whoopy correctly", async function () {

               const whoopyName = await accountConnectedClone.getWhoopyName()
               const num = await accountConnectedClone.getNum()

               expect(whoopyName).to.equal("Test")
               expect(num).to.equal(1)
           })
       })

這是我正在測試的功能:

  function createWhoopy( //to be created only once 
       string memory s_whoopyName,
       uint256 s_num,
   ) public payable restricted {
       whoopyName = s_whoopyName;
       num = s_num;
}

看起來很簡單,但不知道為什麼它不起作用。我的帳戶有足夠的測試 eth。測試還原如下:

AssertionError: expected '' to equal 'Test'
     + expected - actual

     +Test

任何指導將不勝感激!謝謝!

我想到了。這是一件愚蠢的事情(正如我所想的那樣)。我忘了放await create.wait(6)哪個是等待區塊探勘交易所需的。

它在 Hardhat 上通過,因為 Hardhat 立即探勘區塊,但由於 Goerli 是一個實時測試網,你需要等待交易被探勘。

您的 hardhat.config.ts 中有提到 Goerli 網路嗎?如果不嘗試將此添加到您的 hardhat.config.ts goerli: { url: https://eth-goerli.g.alchemy.com/v2/${alchemyApiKey}中的網路, accounts: {mnemonic: mnemonic} },

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