7

假设我有一个以“单元”样式定义的 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)
    }
  }
}

是否有一种简单的方法可以跳过/禁用/标记等待中的所有示例 should block/fragment for myMethod

显然,我可以从块中的每个单独的示例调用pendingUntilFixed或返回pending,但是对于具有许多规范的块来说,这将是相当乏味的。

MyClass.myMethod如果难以实施并受到打击,这似乎是一种常见的情况。在specs2中是否有另一种方法可以做到这一点?

4

1 回答 1

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")
}

然后你运行你的规范exclude pending

>test-only *MyClassSpec* -- exclude pending

在此处记录。

您还可以使用隐式上下文来确保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)
  }
}

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)
     }
}
于 2013-02-20T22:56:24.443 回答