在我们的开发中,我们使用 TDD,所以我们有一些这样的测试:
"User" should {
"return 'Mike' if its name is 'Mike'" in {
val user = User("Mike")
user.getName === "Mike"
}
"return 20 if its age is 20" in {
val user = User(age = Some(20))
user.getAge === Some(20)
}
}
我们在这里编写的测试看起来像“单元测试”。
然后我发现 specs2 提供了另一种更易于表达的语法,我对此很感兴趣:
def is = s2"""
User can have name and age, and we have ways to get them, say, if we have a user whose name is "Mike" and the age is "20",
- we can the name "Mike" [$e1]
- also can get the age 20 [$e2]
"""
def e1 = {
val user = User("Mike")
user.getName === "Mike"
}
def e2 = {
val user = User(age = Some(20))
user.getAge === Some(20)
}
我想用 TDD 试试,但很快我发现这种测试是“验收规范”。一个强烈的问题浮现在我的脑海:
如果我们提到“TDD”,它们是什么类型的测试?它们必须是“单元测试”吗?使用“验收规范”来推动实施是一种好习惯吗?