다음 클래스가 있는데 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/ –
specs2 문서를 검토했다고 가정합니다. Board 클래스를 감안할 때 메소드를 호출하여 예상대로 코드가 작동하는지 테스트를 작성하여 확인하고 상태 또는 리턴 값을 예상대로 확인하십시오. 몇 가지 예를 보려면 https://github.com/mongodb/casbah/tree/master/casbah-gridfs/src/test/scala 여기를 참조하십시오. http://www.youtube.com/watch?v = lMyNRUuEvNU – foolshat
고마워요, 한 번 봤지만 여전히 이해가 안갑니다! 당신은 내게 보드 클래스에 대한 예제를 주실 수 있습니까? 클래스 BoardSpec이 SpecificationWithJUnit을 확장합니다. – user1137701