메인 콘텐츠로 건너뛰기

사전 요구 사항

Hardhat 프로젝트가 이미 설정되어 있고 스마트 컨트랙트가 성공적으로 컴파일되어 있어야 합니다. 방법은 Hardhat 설정 및 스마트 컨트랙트 컴파일 튜토리얼을 참조하세요.

테스트 사양 편집

테스트하는 스마트 컨트랙트가 최소이므로 필요한 테스트 케이스도 최소입니다. 테스트 전에 스마트 컨트랙트를 배포해야 합니다. 이것은 before 블록에서 발생합니다. 스마트 컨트랙트는 단독으로 실행될 수 없으며 EVM 내에서 실행되어야 하기 때문입니다. Hardhat에서는 기본적으로 테스트가 일시적인 에뮬레이트된 인메모리 EVM 인스턴스에서 실행되므로 배포는 형식적입니다. 파일을 여세요: test/Counter.test.js
const { expect } = require('chai');

describe('Counter', function () {
  let counter;

  before(async function () {
    Counter = await ethers.getContractFactory('Counter');
    counter = await Counter.deploy();
    await counter.waitForDeployment();
  });

  it('should start with a count of 0', async function () {
    expect(await counter.value()).to.equal(0);
  });

  it('should increment the count starting from zero', async function () {
    await counter.increment(100);
    expect(await counter.value()).to.equal(100);
  });

  it('should increment the count starting from non-zero', async function () {
    await counter.increment(23);
    expect(await counter.value()).to.equal(123);
  });
});

3개의 테스트 케이스가 있습니다:
  • 초기 value() 확인.
  • increment(num)을 호출한 다음 value()가 업데이트되었는지 확인.
  • increment(num)을 다시 호출한 다음 value()가 다시 업데이트되었는지 확인.

스마트 컨트랙트에 대한 테스트 실행

다음 명령은 방금 살펴본 테스트를 실행합니다.
npx hardhat test
다음 명령은 에뮬레이트된 EVM 인스턴스 내가 아닌 곳에서 테스트를 실행합니다. 대신 스마트 컨트랙트가 Injective 테스트넷(퍼블릭 네트워크)에 배포되고 그에 대해 테스트가 실행됩니다. 대부분의 경우 권장되지 않으며 특정/고급 사용 사례에서만 필요합니다.
npx hardhat test --network inj_testnet

테스트 출력 확인

모든 테스트가 계획대로 작동하면 다음과 유사한 출력이 표시됩니다:
  Counter
    ✔ should start with a count of 0
    ✔ should increment the count starting from zero
    ✔ should increment the count starting from non-zero
  3 passing (41ms)
그 뒤에 복잡성과 트랜잭션 비용의 척도인 가스에 대한 추가 보고가 포함된 테이블이 표시됩니다.

다음 단계

이제 스마트 컨트랙트를 테스트했으므로 해당 스마트 컨트랙트를 배포할 준비가 되었습니다! 다음으로 Hardhat으로 스마트 컨트랙트 배포 튜토리얼을 확인하세요.