ReasonJun

Hardhat : loadFixture(), it(), expect() 본문

Blockchain/HardHat

Hardhat : loadFixture(), it(), expect()

ReasonJun 2023. 10. 20. 15:37
728x90
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { expect } from 'chai';
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 };
  }

  describe('VendingMachine', function () {
    it('should make 100 cupcake at constructor', async function () {
      const { vendingMachine } = await loadFixture(VendingMachineFixture);
      expect(
        (
          await vendingMachine.cupcakeBalances(vendingMachine.address)
        ).toNumber()
      ).to.equal(100);
    });

  });
});

The loadFixture() function is used to load the state that was setup by the VendingMachineFixture() function.

 

The it() function is used to define a test. In this case, the test is checking that the VendingMachine contract has a correct balance of 100 cupcakes.

 

The expect() function is used to make assertions about the state of the test. In this case, the expect() function is asserting that the balance of the VendingMachine contract is equal to 100.

728x90
Comments