如何跳过specs2中的"should"块/片段?

Kev*_*oid 7 scala specs2

假设我在"单元"样式中定义了specs2规范,如下所示:

import org.specs2.mutable

class MyClassSpec extends mutable.Specification {
  "myMethod" should {
    "return positive values" in {
      MyClass.myMethod must beGreaterThan(0)
    }

    "return values less than 100" in {
      MyClass.myMethod must beLessThan(100)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法可以跳过/禁用/标记待定块/片段中的所有示例myMethod

显然,我可以从块中的每个单独的示例调用pendingUntilFixed或返回pending,但对于具有许多规范的块而言,这将是相当繁琐的.

如果MyClass.myMethod难以实施并受到惩罚,这似乎是常见的事情.还有另一种方法,这通常在specs2中完成吗?

Eri*_*ric 7

您可以混合Tags特征并定义section您想要的任何内容:

import org.specs2.mutable._

class MyClassSpec extends Specification with Tags {

  section("pending")
  "myMethod" should {
    "return positive values" in {
      MyClass.myMethod must beGreaterThan(0)
    }

    "return values less than 100" in {
      MyClass.myMethod must beLessThan(100)
    }
  }
  section("pending")
}
Run Code Online (Sandbox Code Playgroud)

然后用你的规范运行 exclude pending

>test-only *MyClassSpec* -- exclude pending
Run Code Online (Sandbox Code Playgroud)

在此处记录.

您还可以使用隐式上下文来确保should块中的所有示例都是PendingUntilFixed:

import org.specs2._
import execute._

class MyClassSpec extends mutable.Specification { 
  "this doesn't work for now" >> {
    implicit val puf = pendingContext("FIXME")
    "ex1" in ko
    "ex2" in ok
  }
  "but this works ok" >> {
    "ex3" in ko // maybe not here ;-)
    "ex4" in ok
  }

  def pendingContext(reason: String) = new mutable.Around {
    def around[T <% Result](t: =>T) = 
      t.pendingUntilFixed(reason)
  }
}
Run Code Online (Sandbox Code Playgroud)

更新specs2 3.x

import org.specs2._
import execute._

class TestMutableSpec extends mutable.Specification {
  "this doesn't work for now" >> {
    implicit def context[T] = pendingContext[T]("FIXME")

    "ex1" in ko
    "ex2" in ok
  }
  "but this works ok" >> {
    "ex3" in ko // maybe not here ;-)
    "ex4" in ok
  }

   def pendingContext[T](reason: String): AsResult[MatchResult[T]] =     
     new AsResult[MatchResult[T]] {
      def asResult(t: =>MatchResult[T]): Result =
        AsResult(t).pendingUntilFixed(reason)
     }
}
Run Code Online (Sandbox Code Playgroud)