2016-12-13 5 views
1

scalatestitキーワードの周りにラッパーを構築しようとしています。しかし、このソリューションは意図したとおりに動作していないようです。また、それもコンパイルされません。次のようにscalatestの "it"キーワードの周りにラッパーを構築する方法は?

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のようなものにはここで満足な解決策ではありません。

私はここで間違っていますか?どんな解決策も高く評価されるでしょう!ありがとう!

+0

、コンパイラが混乱します。次に、実際にそれを無効にするには、val it = new ItWord {...}をオーバーライドする必要があります。その匿名クラスの中で、SuperはFunSpecの古いItWord実装を参照します。 – HTNW

答えて

1

これを試してください:あなたは_itするためにそれをエイリアスしようとするとそこに定義がFunSpecLikeであればだとMyTrait内の別の1(したがって、それが過負荷になっている)ので

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

あなたの解決策を説明する言葉がうまくいくでしょう。 OPはまた、自分のコードに何が間違っているのか尋ねました。 – Borodin

関連する問題