3

我在 Scala (v2.9.1) 上使用 specs2 (v1.8.2) 来编写验收测试。按照http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Contexts的示例,我有以下规范和上下文案例类:

import org.specs2._


class testspec extends SpecificationWithJUnit { def is =

    "test should" ^
        "run a test in a context" ! context().e1

}

case class context() {

    def e1 = 1 must beEqualTo(1)
}

我得到一个编译器错误:

错误:值必须不是 Int def e1 = 1 must beEqualTo(1) 的成员

编译上下文案例类时。

显然我是 specs2(和 Scala)的新手。对适当文档的引用将不胜感激。

4

2 回答 2

3

显然文档是错误的(了,我现在修好了)。

为上下文编写声明案例类的正确方法通常是将其包含在Specification范围内:

import org.specs2._

class ContextSpec extends Specification { def is =
  "this is the first example" ! context().e1

  case class context() {
    def e1 = List(1,2,3) must have size(3)
  }
}

否则,如果您想在另一个规范中重用上下文,您可以像 Dario 所写的那样,MustMatchers通过导入MustMatchers对象方法或继承MustMatchers特征来访问功能。

于 2012-03-12T03:50:04.027 回答
2

must 不是 Int 的成员,因为在您的“context”类的上下文中“must”是未知的。将方法“e1”放在您的规范类中,它应该可以工作。例如

import org.specs2._

class TestSpec extends Specification { def is =
  "test should" ^
    "run a test in a context" ! e1 ^
    end

  def e1 = 1 must beEqualTo(1)

}

编辑

啊,我明白你想要什么;-)。这应该像这样工作:

要将匹配器放在上下文类的范围内,您必须导入 MustMatchers。

import org.specs2._
import matcher.MustMatchers._

class ContextSpec extends Specification { def is =
  "this is the first example" ! context().e1
}

case class context() {
  def e1 = List(1,2,3) must have size(3)
}
于 2012-03-09T17:52:20.810 回答