Search code examples
scalascalatestscalactic

How to build a wrapper around scalatest's "it" keyword?


I am attempting to build a wrapper around scalatest's it keyword. However, this solution does not seem to work as intended. Moreover, it does not even compile:

trait MyFunSpec extends FunSpec {

  private val _it: FunSpec.this.ItWord = it

  protected def it(specText: String, testTags: org.scalatest.Tag*)
    // ...do something extra here...
    (testFun: => Any /* Assertion */)(implicit pos: Position): Unit = {
    _it(specText, testTags: _*)(testFun)(pos)
    // ...do something extra here...
  }
}

The error message I am getting after compiling this code is as follows:

[error] MyFunSpec.scala: ambiguous reference to overloaded definition,
[error] both method it in trait MyFunSpec of type (specText: String, testTags:
  org.scalatest.Tag*)(testFun: => Any)(implicit pos:
  org.scalactic.source.Position)Unit
[error] and value it in trait FunSpecLike of type => FunSpecSpec.this.ItWord
[error] match argument types (String)

Please note the main idea is that method's name remains it, so renaming it to something like alternativeIt is not a satisfactory solution here.

Any suggestions, what am I doing wrong here? Any solution would be highly appreciated! Thanks!


Solution

  • Try this:

    trait MyFunSpec extends FunSpec {
      override protected val it = new ItWord {
        override def apply(specText: String,testTags: org.scalatest.Tag*)(testFun: => Any)(implicit pos: org.scalactic.source.Position): Unit = {
          println("Before")
          super.apply(specText, testTags:_*)(testFun)(pos)
          println("After")
        }
      }
    }