【发布时间】:2012-02-06 22:23:26
【问题描述】:
我有以下课程,我想编写一些 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 的文档:etorreborre.github.com/specs2
-
假设您已经查看了 specs2 文档。给定您的 Board 类,您希望通过调用方法并检查状态或返回值是否符合您的预期来编写测试来确认您的代码的行为是否符合您的预期。看看这里的一些例子github.com/mongodb/casbah/tree/master/casbah-gridfs/src/test/… 也看看这个视频youtube.com/watch?v=lMyNRUuEvNU
-
谢谢,我看了一下,还是不明白!你能给我举个板课的例子吗?类 BoardSpec 扩展 SpecificationWithJUnit { }
标签: unit-testing scala specifications specs specs2