0Pricing
Blockchain Smart Contracts with Solidity · บทเรียน

การทดสอบแบบสุ่มและการทดสอบค่าคงตัว

เรียนรู้ว่าการทดสอบแบบสุ่มที่อิงคุณสมบัติและการทดสอบค่าคงตัวช่วยค้นหาข้อบกพร่องกรณีขอบในสัญญาอัจฉริยะที่การทดสอบหน่วยแบบตายตัวมองข้ามได้อย่างไร โดยใช้ Foundry เป็นกรอบงานตัวอย่าง

การทดสอบแบบสุ่มและการทดสอบค่าคงตัว เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Limits of Example Tests

You have written unit tests with specific inputs. But attackers find the one input you did not try. Fixed examples cannot cover the huge space of possible values. This is where fuzzing helps.

What Is Fuzzing?

Fuzz testing runs a test many times with randomly generated inputs. Instead of asserting on one value, you assert a property that should hold for all inputs.

A Fuzz Test in Foundry

In Foundry, any test function parameter is automatically fuzzed. The framework feeds in many random values.

function testFuzz_DepositIncreasesBalance(uint256 amount) public {
    vm.assume(amount > 0 && amount < 1e30);
    vault.deposit(amount);
    assertEq(vault.balanceOf(address(this)), amount);
}

Bounding Inputs

Random values can be absurd (like the max uint256). Use vm.assume to discard bad inputs or bound to map a value into a valid range so tests stay meaningful.

function testFuzz_Transfer(uint256 amount) public {
    amount = bound(amount, 1, token.balanceOf(address(this)));
    token.transfer(bob, amount);
    assertEq(token.balanceOf(bob), amount);
}

Thinking in Properties

The shift is from 'with input X expect Y' to 'no matter the input, this rule holds'. Common properties:

  • Total supply never changes on a transfer
  • A user can never withdraw more than they deposited
  • Balances never underflow

What Are Invariants?

An invariant is a property that must hold after any sequence of operations, not just one call. Invariant testing fires many random function calls in random order, then checks the invariant after each step.

Declaring an Invariant

In Foundry, functions prefixed with invariant_ are checked after each randomized call sequence.

function invariant_TotalSupplyEqualsSumOfBalances() public {
    assertEq(token.totalSupply(), handler.sumOfBalances());
}

The Handler Pattern

Raw random calls often revert or wander into useless states. A handler contract wraps the target with guided, valid actions and tracks expected totals (ghost variables) for the invariant to check.

contract Handler {
    Token token;
    uint256 public sumOfBalances;

    function transfer(uint256 toSeed, uint256 amount) external {
        // bounded, valid transfer logic that updates ghost totals
    }
}

Shrinking Failures

When a fuzzer finds a failing input, it shrinks it to the simplest counterexample. This makes the bug far easier to understand and reproduce than a random gigantic number.

Tuning Test Runs

More runs find deeper bugs but take longer. Configure run counts in foundry.toml for CI versus quick local checks.

[fuzz]
runs = 1000

[invariant]
runs = 256
depth = 50

When to Use Each

Use fuzzing to harden individual functions against unexpected single inputs. Use invariant testing to verify system-wide rules survive any sequence of actions. Together they catch classes of bugs fixed tests never reach.

Quick Check

Test your understanding of property-based testing.

Recap

You learned fuzzing and invariant testing:

  • Fuzzing runs functions with many random inputs against properties
  • vm.assume and bound keep inputs valid
  • Invariants check system rules across random call sequences
  • Handlers guide actions; shrinking simplifies failures

These techniques uncover edge cases that example-based tests miss.

คำถามที่พบบ่อย

บทเรียน “การทดสอบแบบสุ่มและการทดสอบค่าคงตัว” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทดสอบแบบสุ่มและการทดสอบค่าคงตัว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบแบบสุ่มและการทดสอบค่าคงตัว”

เรียนรู้ว่าการทดสอบแบบสุ่มที่อิงคุณสมบัติและการทดสอบค่าคงตัวช่วยค้นหาข้อบกพร่องกรณีขอบในสัญญาอัจฉริยะที่การทดสอบหน่วยแบบตายตัวมองข้ามได้อย่างไร โดยใช้ Foundry เป็นกรอบงานตัวอย่าง คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การทดสอบแบบสุ่มและการทดสอบค่าคงตัว” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม

ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การทดสอบขั้นสูงด้วย Foundry/Hardhat
  2. พื้นฐานการตรวจสอบอย่างเป็นทางการ
  3. การนำไปใช้งานบนเมนเน็ตและการติดตาม
  4. การทดสอบแบบสุ่มและการทดสอบค่าคงตัว
← กลับไปที่ Blockchain Smart Contracts with Solidity