ReasonJun

Hardhat : describe(), Fixture(), ethers.getSigners() 본문

Blockchain/HardHat

Hardhat : describe(), Fixture(), ethers.getSigners()

ReasonJun 2023. 10. 20. 15:12
728x90
import { ethers } from "hardhat";

describe("VendingMachine", function () {
  // We define a fixture to reuse the same setup in every test.
  // We use loadFixture to run this setup once, snapshot that state,
  // and reset Hardhat Network to that snapshot in every test.
  async function VendingMachineFixture() {
    // Contracts are deployed using the first signer/account by default
    const [owner, otherAccount] = await ethers.getSigners();

    const VendingMachine = await ethers.getContractFactory("VendingMachine");
    const vendingMachine = await VendingMachine.deploy();

    return { vendingMachine, owner, otherAccount };
  }

 
});
  1. describe("VendingMachine", function () { ... });: This defines a test suite named "VendingMachine" using Mocha. Inside this suite, you would typically write multiple test cases to test different functions and behaviors of the "VendingMachine" contract.
  2. async function VendingMachineFixture() { ... }: This is a function named "VendingMachineFixture" that appears to be intended for creating a fixture (a common setup) for your tests. This fixture sets up an instance of the "VendingMachine" contract and provides references to the owner and another account. The await ethers.getSigners() function is used to get a list of available signers (Ethereum accounts) for testing.
  3. Inside the VendingMachineFixture function, the VendingMachine contract is deployed, and the owner and otherAccount are obtained from the available signers.

It's important to note that you need to add actual test cases within the describe block to test the functions and behavior of the "VendingMachine" contract. You can use assertions and ethers functions to interact with the deployed contract and check its state and behavior in your tests.

728x90
Comments