Scala for Backend Engineering & Functional Programming · บทเรียน

ตัวจับคู่

การตรวจสอบเงื่อนไขที่สื่อความหมาย

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

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

Matchers คืออะไร

matchers ของ ScalaTest มีไวยากรณ์สำหรับการตรวจสอบที่สื่อความหมายและคล้ายภาษาอังกฤษ แทนที่จะเขียน assert(x == 3) คุณเขียน x shouldBe 3 ทำให้การทดสอบอ่านง่ายและข้อความเมื่อไม่ผ่านชัดเจน

การผนวก Matchers

ผนวกคุณลักษณะ Matchers เข้ากับข้อกำหนดเพื่อเปิดใช้ DSL ซึ่งใช้ร่วมกับรูปแบบการทดสอบใดก็ได้

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class DemoSpec extends AnyFlatSpec with Matchers {
  "A value" should "equal itself" in {
    val x = 3
    x shouldBe 3
  }
}

Matchers สำหรับความเท่ากัน

ตรวจสอบความเท่ากันด้วย shouldBe หรือ should equal ทั้งสองเปรียบเทียบด้วย == แต่เขียนได้เป็นธรรมชาติ

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class EqSpec extends AnyFlatSpec with Matchers {
  "sum" should "be correct" in {
    (2 + 2) shouldBe 4
    (2 + 2) should equal (4)
  }
}

Matchers สำหรับการเปรียบเทียบ

การเปรียบเทียบตัวเลขอ่านคล้ายร้อยแก้ว: should be >, should be <= และช่วงค่าด้วย be within เพื่อกำหนดค่าความคลาดเคลื่อนของจำนวนทศนิยม

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CmpSpec extends AnyFlatSpec with Matchers {
  "a number" should "compare" in {
    7 should be > 3
    7 should be <= 7
    2.0 shouldBe 2.0 +- 0.01
  }
}

Matchers สำหรับสตริง

Matchers สำหรับสตริงประกอบด้วย startWith, endWith, include และนิพจน์ปกติด้วย fullyMatch regex

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class StrSpec extends AnyFlatSpec with Matchers {
  "a string" should "match parts" in {
    "hello world" should startWith ("hello")
    "hello world" should include ("o w")
    "hello world" should endWith ("world")
  }
}

Matchers สำหรับคอลเลกชัน

ตรวจสอบคอลเลกชันได้อย่างคล่องตัวด้วย have size, contain, contain allOf และตรวจสอบความว่างด้วย shouldBe empty

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ColSpec extends AnyFlatSpec with Matchers {
  "a list" should "have expected contents" in {
    List(1, 2, 3) should have size 3
    List(1, 2, 3) should contain (2)
    List.empty[Int] shouldBe empty
  }
}

Matchers สำหรับค่าบูลีนและ Option

จับคู่ค่าความจริงและเนื้อหาของ Option ได้โดยตรงด้วย shouldBe true, shouldBe defined และ should contain สำหรับค่าภายใน

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class OptSpec extends AnyFlatSpec with Matchers {
  "an option" should "be inspected" in {
    Some(5) shouldBe defined
    Some(5) should contain (5)
    None shouldBe empty
  }
}

การรวม Matchers

รวมเงื่อนไขที่คาดหวังด้วย and / or เพื่อสร้างการตรวจสอบแบบผสม

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CombineSpec extends AnyFlatSpec with Matchers {
  "a number" should "satisfy both" in {
    7 should (be > 0 and be < 10)
  }
}

การทดสอบข้อยกเว้น

ใช้ an [Exception] should be thrownBy { ... } หรือ assertThrows เพื่อตรวจสอบว่าโค้ดล้มเหลวตามที่คาดไว้

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ExSpec extends AnyFlatSpec with Matchers {
  "dividing by zero" should "throw" in {
    an [ArithmeticException] should be thrownBy (1 / 0)
  }
}

เหตุผลที่ Matchers มีประโยชน์

นอกจากช่วยให้อ่านง่ายแล้ว matchers ยังสร้างข้อความเมื่อไม่ผ่านที่อธิบายได้ชัดเจน การทดสอบ List(1,2) should have size 3 ที่ไม่ผ่านจะแจ้งขนาดที่คาดไว้และขนาดจริง ต่างจาก assert ธรรมดาที่บอกเพียงว่าค่าบูลีนเป็นเท็จ

shouldBe กับ should equal

ทั้งสองแทบเหมือนกัน shouldBe กระชับกว่าและให้ข้อความที่สะอาดกว่าเล็กน้อยสำหรับการตรวจสอบความเท่ากันแบบง่าย ส่วน should equal รองรับอินสแตนซ์ Equality ที่กำหนดเองสำหรับการเปรียบเทียบขั้นสูง

ตรวจสอบความเข้าใจอย่างรวดเร็ว

ทดสอบความรู้เกี่ยวกับ matchers

สรุปทบทวน

คุณได้เรียนรู้เรื่องmatchers ของ ScalaTest:

  • ผนวก Matchers เพื่อใช้ DSL
  • ความเท่ากัน: shouldBe, should equal; การเปรียบเทียบด้วย be >, +- tolerance
  • สตริง: startWith, include; คอลเลกชัน: have size, contain
  • ข้อยกเว้น: should be thrownBy
  • Matchers ให้ผลลัพธ์เมื่อไม่ผ่านที่ชัดเจนและอธิบายได้
เริ่มต้นได้ฟรี

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

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

คอร์ส
39
บทเรียน
143

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

บทเรียน “ตัวจับคู่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวจับคู่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Scala for Backend Engineering & Functional Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Scala for Backend Engineering & Functional Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวจับคู่”

การตรวจสอบเงื่อนไขที่สื่อความหมาย คุณปฏิบัติ Scala for Backend Engineering & Functional Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Scala for Backend Engineering & Functional Programming หรือไม่

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

บทเรียน “ตัวจับคู่” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Scala for Backend Engineering & Functional Programming นี้ได้ไหม

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

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

  1. รูปแบบของ ScalaTest
  2. ตัวจับคู่
  3. การทดสอบตามคุณสมบัติ
  4. การจำลองและชุดข้อมูลทดสอบ
← กลับไปที่ Scala for Backend Engineering & Functional Programming