我有下面的类,我想写一些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
}
}谢谢!
发布于 2012-01-11 05:56:40
我只能赞同Daniel的建议,因为通过使用TDD,您最终会得到一个更实用的API。
我还认为您的应用程序可以通过混合使用specs2和ScalaCheck进行很好的测试。下面是一个规范草案,让您开始使用:
import org.specs2._
import org.scalacheck.{Arbitrary, Gen}
class TestSpec extends Specification with ScalaCheck { def is =
"moving a coin in a column moves the coin to the nearest empty slot" ! e1^
"a coin wins if" ^
"a row contains 4 consecutive coins" ! e2^
"a column contains 4 consecutive coins" ! e3^
"a diagonal contains 4 consecutive coins" ! e4^
end
def e1 = check { (b: Board, x: Int, c: Coin) =>
try { b.move(x, c) } catch { case e => () }
// either there was a coin before somewhere in that column
// or there is now after the move
(0 until 6).exists(y => b(x, y).isDefined)
}
def e2 = pending
def e3 = pending
def e4 = pending
/**
* Random data for Coins, x position and Board
*/
implicit def arbitraryCoin: Arbitrary[Coin] = Arbitrary { Gen.oneOf(Cross, Naught) }
implicit def arbitraryXPosition: Arbitrary[Int] = Arbitrary { Gen.choose(0, 6) }
implicit def arbitraryBoardMove: Arbitrary[(Int, Coin)] = Arbitrary {
for {
coin <- arbitraryCoin.arbitrary
x <- arbitraryXPosition.arbitrary
} yield (x, coin)
}
implicit def arbitraryBoard: Arbitrary[Board] = Arbitrary {
for {
moves <- Gen.listOf1(arbitraryBoardMove.arbitrary)
} yield {
val board = new Board
moves.foreach { case (x, coin) =>
try { board.move(x, coin) } catch { case e => () }}
board
}
}
}
object Cross extends Coin {
override def toString = "x"
}
object Naught extends Coin {
override def toString = "o"
}
sealed trait Coin我实现的e1属性不是真正的属性,因为它不会真正检查我们是否将硬币移动到最近的空槽中,这是您的代码和应用程序接口所建议的。您还需要更改生成的数据,以便用x和o交替生成电路板。这应该是学习如何使用ScalaCheck的一个很好的方法!
发布于 2012-01-10 23:32:13
我建议您丢弃所有这些代码--好吧,将它保存在某个地方,但是使用TDD从零开始。
Specs2站点上有很多关于如何编写测试的示例,但是使用测试驱动设计来完成测试。至少可以说,在事实之后添加测试是次优的。
因此,想一想你想要处理的最简单的功能的最简单的情况,为此编写一个测试,看到它失败,编写代码来修复它。必要时重构,并对下一个最简单的情况重复此步骤。
如果你需要关于如何做测试开发的一般帮助,我非常支持Clean Coders上关于测试开发的视频。至少,在第二部分中,Bob Martin以TDD方式编写了一个完整的类,从设计到结束。
如果你大体上知道如何做测试,但对Scala或规范感到困惑,请详细说明你的问题。
https://stackoverflow.com/questions/8804431
复制相似问题