匹配器
表达力强的断言
匹配器 是 CoddyKit 上的免费 Scala for Backend Engineering & Functional Programming 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Scala for Backend Engineering & Functional Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
什么是匹配器
ScalaTest 的匹配器提供了富有表现力、类似英语的断言语法。您无需编写 assert(x == 3),而是可以写成 x shouldBe 3,从而生成易读的测试和清晰的失败消息。
混入匹配器
将 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
}
}相等匹配器
使用 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)
}
}比较匹配器
数值比较读起来像普通句子: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
}
}字符串匹配器
字符串匹配器包括 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")
}
}集合匹配器
您可以流畅地检查集合:使用 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
}
}布尔值和 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
}
}组合匹配器
使用 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)
}
}匹配器的作用
除了提高可读性之外,匹配器还会生成描述性失败消息。失败的 List(1,2) should have size 3 会报告预期大小和实际大小;而单独使用 assert 时,只会说明布尔值为 false。
shouldBe 与 should equal
两者几乎完全相同;对于简单的相等性比较,shouldBe 更简洁,消息也略微更清晰,而 should equal 支持用于高级比较的自定义 Equality 实例。
快速检查
检查您对匹配器的了解。
回顾
您学习了 ScalaTest 的匹配器:
- 混入
Matchers以使用 DSL。 - 相等性:
shouldBe、should equal;比较使用be >、+- tolerance。 - 字符串:
startWith、include;集合:have size、contain。 - 异常:
should be thrownBy。 - 匹配器会提供清晰且具有描述性的失败信息。
用 AI 导师学习 Scala — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 39
- 课程
- 143
常见问题解答
「匹配器」课时是免费的吗?
是的 — 「匹配器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Scala for Backend Engineering & Functional Programming 课程的其余内容,请升级到 CoddyKit PRO。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
「匹配器」这节课中我会学到什么?
表达力强的断言 你通过在浏览器中直接运行的动手代码来练习 Scala for Backend Engineering & Functional Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Scala for Backend Engineering & Functional Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Scala for Backend Engineering & Functional Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「匹配器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Scala for Backend Engineering & Functional Programming 课中编写并运行代码吗?
能。每节 Scala for Backend Engineering & Functional Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。