7

Suppose I have a specs2 specification defined in the "unit" style as follows:

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

Is there an easy way to skip/disable/mark pending all of the examples within the should block/fragment for myMethod?

Obviously I can call pendingUntilFixed or return pending from each individual example in the block, but this would be rather tedious for a block with many specifications.

It seems like this would be a common occurrence if MyClass.myMethod is difficult to implement and gets punted. Is there another way that this is commonly done in specs2?

Kevinoid
  • 4,180
  • 40
  • 25

1 Answers1

7

You can mix in the Tags trait and define any section you want:

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

Then you run your specification with exclude pending

>test-only *MyClassSpec* -- exclude pending

This is documented here.

You can also use an implicit context to make sure that all your examples in the should block are 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)
  }
}

Update for 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)
     }
}
Eric
  • 15,494
  • 38
  • 61
  • Sure, that'll do. Thanks! – Kevinoid Feb 20 '13 at 23:05
  • I don't suppose there is a way to make the fragments show up as pending in the output? When they are excluded, they become a bit more difficult to remember to fix (at least, for me). – Kevinoid Feb 21 '13 at 16:58
  • Hi @Eric is it possible to do this with the (implicit: ev T => Result) syntax? I gave it a shot but couldn't find anything that worked. Also thanks for always supporting your library! Nice work :-) – jpswain Jan 24 '16 at 02:30
  • Which version are you using? I'm updating the question with an answer for 3.x. – Eric Jan 29 '16 at 03:32