scala - 在 Scala Specs 中, "must"函数是什么?

标签 scala specs

我正在进行一些 Specs 测试,并试图了解“必须”功能是什么,以及它的作用。

我无法在规范源中的任何地方找到它的声明或实现,我正在尝试了解它的作用。

以下是它的一些示例用法:

"hello world".size must be equalTo(11)
"hello world" must be matching("h.* w.*")
stack.push(11) must throwAn[Error]

在我看来,“必须”将函数作为参数,但我想知道“必须”的实际签名,以及它对参数的作用。

谁能指出我正确的方向?

谢谢!

最佳答案

至少在 Specs2.0 中,你会在 org.specs2.matcher.MustExpectable 中找到 must 的定义。

/**
 * This kind of expectable can be followed by the verb must to apply a matcher:
 * 
 * `1 must beEqualTo(1)`
 * 
 * For convenience, several mustMatcher methods have also been defined as shortcuts to equivalent:
 * 
 * `a must matcher`
 */
class MustExpectable[T] private[specs2] (tm: () => T) extends Expectable[T](tm) { outer =>
  def must(m: =>Matcher[T]) = applyMatcher(m)
  def must_==(other: =>T) = applyMatcher(new BeEqualTo(other))
  def must_!=(other: =>T) = applyMatcher(new BeEqualTo(other).not)
}
object MustExpectable {
  def apply[T](t: =>T) = new MustExpectable(() => t)
}

MustExpectation trait 在这里声明相关的隐式转换:

/**
 * This trait provides implicit definitions to transform any value into a MustExpectable
 */
trait MustExpectations extends Expectations {
  implicit def akaMust[T](tm: Expectable[T]) = new MustExpectable(() => tm.value) {
    override private[specs2] val desc = tm.desc
  }
  implicit def theValue[T](t: =>T): MustExpectable[T] = createMustExpectable(t)
  implicit def theBlock(t: =>Nothing): MustExpectable[Nothing] = createMustExpectable(t)

  protected def createMustExpectable[T](t: =>T) = MustExpectable(t)
}
object MustExpectations extends MustExpectations

关于scala - 在 Scala Specs 中, "must"函数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5409615/

相关文章:

scala - akka中的死信箱可以重复使用吗?

json - 使用 Specs2 测试 json 数据

scala - 组合函数导致 StackOverflow

javascript - 使用 Jasmine Specs 比较对象

scala - 在ScalaTest中使用 “should NOT produce [exception]”语法

ruby-on-rails - 创建自定义 rspec 变量,如 Controller 规范的响应

cuda - 如何在 CUDA 中以编程方式获取卡规范

scala - 自定义 Scala 枚举,搜索到的最优雅的版本

scala - 使用 Akka-Streams HTTP 将整个 HttpResponse 主体作为字符串获取

maven-2 - 获取有关失败的 scala/maven/specs 测试的失败详细信息