我已經得到了以下類,我想寫一些Spec測試用例,但我真的很陌生,我不知道如何開始。我的班級喜歡這樣:斯卡拉規格單元測試
class Board{
val array = Array.fill(7)(Array.fill(6)(None:Option[Coin]))
def move(x:Int, coin:Coin) {
val y = array(x).indexOf(None)
require(y >= 0)
array(x)(y) = Some(coin)
}
def apply(x: Int, y: Int):Option[Coin] =
if (0 <= x && x < 7 && 0 <= y && y < 6) array(x)(y)
else None
def winner: Option[Coin] = winner(Cross).orElse(winner(Naught))
private def winner(coin:Coin):Option[Coin] = {
val rows = (0 until 6).map(y => (0 until 7).map(x => apply(x,y)))
val cols = (0 until 7).map(x => (0 until 6).map(y => apply(x,y)))
val dia1 = (0 until 4).map(x => (0 until 6).map(y => apply(x+y,y)))
val dia2 = (3 until 7).map(x => (0 until 6).map(y => apply(x-y,y)))
val slice = List.fill(4)(Some(coin))
if((rows ++ cols ++ dia1 ++ dia2).exists(_.containsSlice(slice)))
Some(coin)
else None
}
override def toString = {
val string = new StringBuilder
for(y <- 5 to 0 by -1; x <- 0 to 6){
string.append(apply(x, y).getOrElse("_"))
if (x == 6) string.append ("\n")
else string.append("|")
}
string.append("0 1 2 3 4 5 6\n").toString
}
}
謝謝!
您可以查看specs2的文檔:http://etorreborre.github.com/specs2/ – 2012-01-10 15:10:13
假設您查看了specs2文檔。考慮到你的Board類,你想通過編寫測試來確認你的代碼的行爲如你所期望的那樣通過調用方法並檢查狀態或返回值是否如你所期望的那樣。看看這裏的一些例子https://github.com/mongodb/casbah/tree/master/casbah-gridfs/src/test/scala另請看這個視頻http://www.youtube.com/watch?v = lMyNRUuEvNU – foolshat 2012-01-10 15:26:33
謝謝,我看了一下,但還是不明白!你可以給我一個板級的例子嗎? class BoardSpec擴展SpecificationWithJUnit { } – user1137701 2012-01-10 15:39:14