FunSpec
旨在提供最小的结构,因此这里没有硬规则。固执己见结构的一个例子是
WordSpec
. 我的一个建议是,通过一个外部的
describe("A ByteSource")
:
class ByteSourceTest extends FunSpec with Matchers {
describe("A ByteSource") {
describe("when empty") {
val byteSource = new ByteSource(new Array[Byte](0))
it("should have size 0") {
assert(byteSource.src.size == 0)
}
it("should produce NoSuchElementException when head is invoked") {
assertThrows[NoSuchElementException] {
byteSource.src.head
}
}
}
describe("when non-empty") {
val byteSource = new ByteSource(new Array[Byte](10))
it("should have size > 0") {
assert(byteSource.src.size > 0)
}
it("should not produce NoSuchElementException when head is invoked") {
noException shouldBe thrownBy {
byteSource.src.head
}
}
}
}
}
有了测试对象,输出看起来像是自然语言中的规范:
A ByteSource
when empty
- should have size 0
- should produce NoSuchElementException when head is invoked
when non-empty
- should have size > 0
- should not produce NoSuchElementException when head is invoked