在第一篇文章我們說過 Specs 可以有兩種風(fēng)格的測試規(guī)范:單元測試規(guī)范和驗(yàn)收測試規(guī)范,我們來看其中的一個例子:
def is = s2"""
this is my specification
and example 1 $e1
and example 2 $e2
"""
def e1 = success
def e2 = success
這段代碼從 S2 字符串創(chuàng)建一組 Fragments(測試用例)對象,這個例子創(chuàng)建了一個 Text 對象和兩個 Example 對象(它們都是 Fragment 的子類)。
在 Specs2 的 Example 對象為一個文字加上任意可以轉(zhuǎn)換成 Result 對象(org.specs2.execute.Result)的對象,它可以是:
最簡單的 Result 值由 StandardResults Trait 定義,可以有如下幾種值:
此外還有兩種情況:
通常 Example 定義體使用 Matcher 定義期望值:
def e1 = 1 must_== 1
Matcher 我們在后面繼續(xù)詳細(xì)介紹。