それを呼び出すメソッドのテストをしたいと思いerror()
ます。
IntEmptyStack.top
specs2でテストしたいのは次のとおりです。
abstract class IntStack {
def push(x: Int): IntStack = new IntNonEmptyStack(x, this)
def isEmpty: Boolean
def top: Int
def pop: IntStack
}
class IntEmptyStack extends IntStack {
def isEmpty = true
def top = error("EmptyStack.top")
def pop = error("EmptyStack.pop")
}
そして、これまでに書いた仕様は次のとおりです。
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
import org.specs2.mutable.Specification
@RunWith(classOf[JUnitRunner])
class IntStackSpec extends Specification {
"IntEmptyStack" should {
val s = new IntEmptyStack
"be empty" in {
s.isEmpty must equalTo(true)
}
"raise error when top called" in {
s.top must throwA[RuntimeException]
}
}
}
エラーは 13 行目で発生します"raise error when top called" in {
。エラー メッセージは次のとおりですvalue must is not a member of Nothing
。s.top
Scala は、抽象クラスで定義されている Int ではなく、Nothing として推論すると思います。この場合、エラーなしでテストを作成するにはどうすればよいでしょうか?
この質問に対するコメント/修正をありがとう。
例の参照: 例による Scala