我想对调用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
。我认为 Scala 推断s.top
为 Nothing,而不是抽象类中定义的 Int。在这种情况下,我怎样才能编写一个没有任何错误的测试?
感谢您对此问题的任何评论/更正。
示例参考:Scala By Example