SwiftUI Academy · บทเรียน

การทดสอบด้วยภาพบันทึกและพรีวิว

ตรวจจับความผิดเพี้ยนด้านภาพในสถานะต่าง ๆ

บทเรียน 4 จาก 413 ขั้นตอน

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

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

Catching Visual Bugs

Some bugs are not crashes; they are layout slips you only see. Snapshot tests catch these by comparing images of your views. 👀

How Snapshots Work

A snapshot test renders a view to an image once, saves it, then fails later if a new render no longer matches.

The Reference Image

The first run records a trusted reference image. You review it by eye, and from then on it becomes the source of truth.

Previews Drive Testing

SwiftUI previews already show a view in many states. Designing those states first makes your snapshots easy to write.

#Preview { ProfileCard(user: .sample) }

Cover Many States

Add previews for empty, loading, and error states. Each one becomes a snapshot, so regressions in any state are caught.

Test Light and Dark

Render each view in light and dark mode. A color that vanishes in dark mode is exactly what a snapshot will reveal. 🌗

.preferredColorScheme(.dark)

Vary Dynamic Type

Snapshot a view at the largest Dynamic Type size too. Text that overflows or clips will fail the comparison immediately.

Pin the Device Size

Always snapshot on a fixed device or frame. Different screen sizes produce different images and cause false failures.

Reading a Failure

When a snapshot fails, the tool shows old, new, and a diff. You decide: a real bug, or an intended change to re-record.

Commit Your Snapshots

Store reference images in version control. Teammates then share the same baseline, and CI can catch visual changes in pull requests.

Re-Record on Purpose

Changed the design intentionally? Switch to record mode once to refresh the baselines, then turn it off again.

Quick Check

What does a snapshot test actually compare?

Recap

You learned snapshot testing: record a reference, cover many preview states, test light and dark, and commit baselines so visual bugs never sneak in. 🎉

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

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

บทเรียน “การทดสอบด้วยภาพบันทึกและพรีวิว” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบด้วยภาพบันทึกและพรีวิว”

ตรวจจับความผิดเพี้ยนด้านภาพในสถานะต่าง ๆ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

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

บทเรียน “การทดสอบด้วยภาพบันทึกและพรีวิว” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

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

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

  1. การทดสอบหน่วยของ ViewModels
  2. การทดสอบโค้ดแบบอะซิงโครนัส
  3. การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest
  4. การทดสอบด้วยภาพบันทึกและพรีวิว
← กลับไปที่ SwiftUI Academy