การทดสอบด้วย forge
การทดสอบ Solidity
การทดสอบด้วย forge เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Tests Are Contracts
In Foundry a test file is a Solidity contract that inherits from Test in the forge-std library. Each public function whose name starts with test is run as an individual test case.
Test files live in test/ and conventionally use the .t.sol suffix.
import {Test} from "forge-std/Test.sol";
contract CounterTest is Test {
function testInitialZero() public {
// ...
}
}The setUp Function
The special setUp() function runs before every test, giving each test a fresh, isolated state. Deploy your contracts and seed any fixtures here.
Because state resets between tests, one test can never accidentally depend on another.
Counter counter;
function setUp() public {
counter = new Counter();
}Assertions
forge-std provides assertion helpers that fail the test with a clear message when they do not hold:
assertEq(a, b)— equalityassertTrue(cond)/assertFalse(cond)assertGt,assertLt,assertGe,assertLe
function testIncrement() public {
counter.increment();
assertEq(counter.number(), 1);
}Running Tests
Run the whole suite with forge test. Filter by name with --match-test or by contract with --match-contract. Increase verbosity with -v flags to see logs and traces.
$ forge test
$ forge test --match-test testIncrement -vvvExpecting Reverts
Use vm.expectRevert immediately before a call that should fail. The test passes only if the next call reverts, optionally matching a specific error message or custom error.
function testOnlyOwner() public {
vm.prank(address(0xBEEF));
vm.expectRevert("not owner");
counter.adminReset();
}Impersonating Accounts
vm.prank(addr) sets msg.sender for the very next call. vm.startPrank(addr) applies it to all calls until vm.stopPrank(). This lets you test access control without deploying from many keys.
vm.startPrank(alice);
token.approve(spender, 100);
token.transfer(bob, 50);
vm.stopPrank();Manipulating Balances and Time
Cheatcodes let you control the environment:
vm.deal(addr, amount)— set an account's ETH balancevm.warp(timestamp)— set block.timestampvm.roll(blockNum)— set block.number
These make time-dependent and balance-dependent logic easy to test deterministically.
vm.deal(alice, 10 ether);
vm.warp(block.timestamp + 7 days);
vm.roll(block.number + 100);Testing Events
vm.expectEmit declares which event fields to check, followed by the expected event, then the call that should emit it. The booleans select which topics and data to match.
vm.expectEmit(true, true, false, true);
emit Transfer(alice, bob, 100);
token.transfer(bob, 100);Labels and Logs
vm.label(addr, name) gives addresses readable names in traces. console.log from forge-std prints debug output during tests when run with sufficient verbosity.
import {console} from "forge-std/console.sol";
vm.label(alice, "Alice");
console.log("balance:", token.balanceOf(alice));Fork Testing
Foundry can fork a live network so your tests run against real deployed contracts and state. Set an RPC URL and use vm.createSelectFork to pin a block.
This is invaluable for testing integrations with protocols like Uniswap or Aave without redeploying them.
function setUp() public {
vm.createSelectFork(vm.rpcUrl("mainnet"), 19000000);
}Test Organization
Good practices for forge tests:
- One behavior per test function
- Descriptive names like
testRevertWhenNotOwner - Shared setup in
setUp() - Use prank for access control, warp for time logic
- Add fork tests for external integrations
Quick Check
What is the purpose of the setUp() function in a Foundry test contract?
Recap
You learned forge testing:
- Tests are Solidity contracts inheriting
Test; functions start withtest setUp()gives fresh state per test- Assertions:
assertEq,assertTrue, etc. - Cheatcodes:
prank,deal,warp,expectRevert,expectEmit - Fork tests run against live network state
Next: fuzzing and invariant testing.
เรียนรู้ Web3 & DApp Development Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 29
- บทเรียน
- 105
คำถามที่พบบ่อย
บทเรียน “การทดสอบด้วย forge” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบด้วย forge” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบด้วย forge”
การทดสอบ Solidity คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบด้วย forge” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Foundry เทียบกับ Hardhat
- การทดสอบด้วย forge
- การทำ fuzz และอินวาเรียนต์
- cast และ anvil