การเขียนการทดสอบ
Mocha และ Chai
การเขียนการทดสอบ เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Test Contracts
Smart contracts are immutable once deployed and often hold real value. A single bug can be unrecoverable, so thorough testing is essential.
- Catch logic errors before deployment.
- Document expected behavior.
- Guard against regressions when refactoring.
Mocha and Chai
Hardhat uses Mocha as the test runner and Chai for assertions, both included in the Toolbox.
describegroups related tests.itdefines a single test case.expectmakes assertions about values.
Tests live in the test/ folder.
A First Test File
A basic test imports ethers and Chai, then deploys and checks the contract:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Greeter", function () {
it("returns the initial greeting", async function () {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy();
expect(await greeter.greeting()).to.equal("Hello, Hardhat");
});
});const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Greeter", function () {
it("returns the initial greeting", async function () {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy();
expect(await greeter.greeting()).to.equal("Hello, Hardhat");
});
});Running Tests
Run the whole suite with:
npx hardhat testHardhat compiles contracts first, spins up a fresh in-process network, and reports passing and failing tests. You can pass a path to run just one file.
npx hardhat testAsync and await
Almost every contract interaction is asynchronous because it talks to the network. Always await deployments and calls:
it("works", async function () {
const c = await Factory.deploy();
await c.waitForDeployment();
const value = await c.getValue();
expect(value).to.equal(0);
});Forgetting await is the most common cause of confusing test results.
it("works", async function () {
const c = await Factory.deploy();
await c.waitForDeployment();
const value = await c.getValue();
expect(value).to.equal(0);
});Common Chai Matchers
Chai gives expressive matchers for assertions:
.to.equal(x)— strict equality..to.be.true/.to.be.false— booleans..to.be.gt(n)— greater than..to.deep.equal([...])— array/object equality.
expect(await token.balanceOf(user)).to.equal(100);expect(await token.balanceOf(user)).to.equal(100);Working with Signers
getSigners returns test accounts you can use as different users:
const [owner, alice, bob] = await ethers.getSigners();
await token.connect(alice).transfer(bob.address, 50);connect(signer) sends the next call as that account, which is key for testing access control.
const [owner, alice, bob] = await ethers.getSigners();
await token.connect(alice).transfer(bob.address, 50);BigInt Values
Numbers on-chain are large integers. In ethers v6 they are JavaScript BigInt values. Compare them carefully:
const supply = await token.totalSupply();
expect(supply).to.equal(ethers.parseEther("1000"));Use parseEther and formatEther to convert between human and on-chain units.
const supply = await token.totalSupply();
expect(supply).to.equal(ethers.parseEther("1000"));Setup with beforeEach
Use beforeEach to deploy a fresh contract for every test, ensuring isolation:
let token;
beforeEach(async function () {
const Token = await ethers.getContractFactory("Token");
token = await Token.deploy();
});Each it then starts from a clean, identical state.
let token;
beforeEach(async function () {
const Token = await ethers.getContractFactory("Token");
token = await Token.deploy();
});Reading Test Output
A green checkmark means the test passed; a red cross with a diff shows what failed. Mocha prints the describe and it labels, so write descriptive names:
- Good:
it("reverts when caller is not owner") - Vague:
it("test1")
Running a Single Test
While developing you often want to focus on one test. Use Mocha's .only:
it.only("reverts when not owner", async function () {
// only this test runs
});You can also run a single file with npx hardhat test test/Token.js. Remember to remove .only before committing.
it.only("reverts when not owner", async function () {
// only this test runs
});Quick Check
Test your understanding of writing contract tests.
Recap
You learned to write contract tests.
- Hardhat uses Mocha (
describe/it) and Chai (expect). - Tests live in
test/and run withnpx hardhat test. - Always
awaitasync contract calls. getSigners+connectsimulate different users.- On-chain numbers are BigInt; use
parseEther/formatEtherandbeforeEachfor clean setup.
คำถามที่พบบ่อย
บทเรียน “การเขียนการทดสอบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเขียนการทดสอบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเขียนการทดสอบ”
Mocha และ Chai คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การเขียนการทดสอบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ