如何围绕 scalatest 的 "it" 关键字构建包装器?

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

我正在尝试围绕 scalatestit 关键字构建一个包装器。但是,此解决方案似乎没有按预期工作。而且,它甚至不编译:

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

我编译这段代码后得到的错误信息如下:

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

请注意,主要思想是方法的名称仍然是 it,因此将其重命名为 alternativeIt 之类的名称并不是一个令人满意的解决方案。

任何建议,我在这里做错了什么?任何解决方案将不胜感激!谢谢!

试试这个:

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