我已经得到了以下类,我想写一些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