ReasonJun

Hardhat : .to.emit().withArgs(); 본문

Blockchain/HardHat

Hardhat : .to.emit().withArgs();

ReasonJun 2023. 10. 20. 15:53
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 send cupcakes correctly after purchase', async function () {
      const { vendingMachine, owner, otherAccount } = await loadFixture(
        VendingMachineFixture
      );
      console.log(
        'vendingMachine cupcakeBalances',
        await vendingMachine.cupcakeBalances(vendingMachine.address)
      );
      const accountTwoStartingBalance = (
        await vendingMachine.cupcakeBalances(otherAccount.address)
      ).toNumber();

      console.log('accountTwoStartingBalance >>', accountTwoStartingBalance);
      const amount = 10;
      await expect(
        vendingMachine
          .connect(otherAccount)
          .purchase(amount, { value: (amount * 10 ** 18).toString() })
      )
        .to.emit(vendingMachine, 'Purchase')
        .withArgs(otherAccount.address, amount);

      const accountTwoEndingBalance = (
        await vendingMachine.cupcakeBalances(otherAccount.address)
      ).toNumber();
      console.log('accountTwoEndingBalance >>', accountTwoEndingBalance);
      console.log(
        'vendingMachine cupcakeBalances',
        await vendingMachine.cupcakeBalances(vendingMachine.address)
      );
      expect(accountTwoEndingBalance).to.equal(
        accountTwoStartingBalance + amount
      );
    });

   
  });
});
.to.emit(vendingMachine, 'Purchase')
    .withArgs(otherAccount.address, amount);
  • .to.emit(vendingMachine, 'Purchase'): This is a Chai assertion statement. It checks that a specific event, in this case, the 'Purchase' event, is emitted when a certain action is performed on the vendingMachine contract. The .to.emit() syntax is used to make this assertion.

  • .withArgs(otherAccount.address, amount): This is an additional part of the assertion. It specifies that the 'Purchase' event should have specific arguments when it is emitted. In this case, it checks that the event should have two arguments: otherAccount.address and amount.

In your test case, you are verifying that when the purchase function is called on the vendingMachine contract, it should emit a 'Purchase' event with the expected arguments - the address of otherAccount and the specified amount.


728x90
Comments