Matchers
表現力の高いアサーションです。
「Matchers」はCoddyKit上の無料Scala for Backend Engineering & Functional Programmingレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはScala for Backend Engineering & Functional Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What are Matchers?
ScalaTest matchers provide an expressive, English-like syntax for assertions. Instead of assert(x == 3), you write x shouldBe 3, producing readable tests and clear failure messages.
Mixing in Matchers
Mix the Matchers trait into your spec to unlock the DSL. It combines with any testing style.
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
}
}Equality Matchers
Check equality with shouldBe or should equal. Both compare with == but read naturally.
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)
}
}Comparison Matchers
Numeric comparisons read like prose: should be >, should be <=, and ranges with be within for floating point tolerance.
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
}
}String Matchers
Matchers for strings include startWith, endWith, include, and regex with 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")
}
}Collection Matchers
Inspect collections fluently: have size, contain, contain allOf, and emptiness with 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
}
}Boolean and Option Matchers
Match truthiness and Option contents directly: shouldBe true, shouldBe defined, and should contain for the inner value.
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
}
}Combining Matchers
Combine expectations with and / or for compound assertions.
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)
}
}Testing for Exceptions
Use an [Exception] should be thrownBy { ... } or assertThrows to verify that code fails as expected.
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)
}
}Why Matchers Help
Beyond readability, matchers produce descriptive failure messages. A failed List(1,2) should have size 3 reports the expected and actual size, unlike a bare assert that only says the boolean was false.
shouldBe vs should equal
They are nearly identical; shouldBe is terser and gives slightly cleaner messages for simple equality, while should equal supports custom Equality instances for advanced comparisons.
Quick Check
Test your knowledge of matchers.
Recap
You learned ScalaTest matchers:
- Mix in
Matchersfor the DSL. - Equality:
shouldBe,should equal; comparisons withbe >,+- tolerance. - Strings:
startWith,include; collections:have size,contain. - Exceptions:
should be thrownBy. - Matchers give clear, descriptive failures.
よくある質問
「Matchers」レッスンは無料ですか?
はい。「Matchers」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Scala for Backend Engineering & Functional Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
「Matchers」で何を学びますか?
表現力の高いアサーションです。 ブラウザで直接実行するハンズオンコードでScala for Backend Engineering & Functional Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Scala for Backend Engineering & Functional Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのScala for Backend Engineering & Functional Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Matchers」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このScala for Backend Engineering & Functional Programmingレッスンでコードを書いて実行できますか?
はい。すべてのScala for Backend Engineering & Functional Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。