Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip a "should" block/fragment in specs2?

Tags:

scala

specs2

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?

like image 261
Kevinoid Avatar asked Feb 20 '13 04:02

Kevinoid


1 Answers

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)
     }
}
like image 156
Eric Avatar answered Oct 25 '22 15:10

Eric