这是 ScalaTest 中的错误吗
它演示了方法重载的限制。
在一篇关于 selfless traits 的文章中,Bill Venners 将这种模式描述为此类命名冲突的解决方法。
在您的情况下,首选一个重载,因为它是在“派生类”中定义的。
(我认为;我不是这些测试框架的用户,并且生成了其中一个源等,因此测试这不是启动 sbt 并查看代码的简单问题。)
(编辑:scaladoc说你应该这样做import Inspectors._
。也许你希望继承它,Matchers
因为它还建议导入它的同伴,尽管这对我来说并不方便。如果你这样做了import Inspectors._
,你实际上不能通过导入名称。)
(编辑:解释命名位:参见规范第 2 章的开头,其中说名称绑定具有优先级,并且您继承的名称具有比您导入的名称更高的优先级。)
无论如何,一种解决方案是重新混合,Inspectors
如下所示。
另一种解决方案是通过重命名导入方法:
import Inspectors.{ forAll => iforAll }
尝试选项“-Xprint:typer”、“-Xlog-implicit-conversions”来查看发生了什么很有用。Gen.value
在您的情况下, ScalaCheck 1.10 中的隐式视图将您的集合提升为“常量代” 。
import org.scalatest._
import org.scalatest.prop._
//class MySpec extends FeatureSpec with Matchers with GeneratorDrivenPropertyChecks
class MySpec extends FeatureSpec with Matchers with GeneratorDrivenPropertyChecks with Inspectors {
case class Foo(id: Int)
val items = 1 to 10 map (Foo.apply(_))
forAll(items) { x => Console println x.id }
}
object Test extends App {
case class Foo(id: Int)
val items = 1 to 10 map (Foo.apply(_))
val sut = new MySpec
sut.forAll(items) { x => Console println x.id }
//sut.forAll[Foo](items) { x => Console println x.i }
}
一些调试输出:
/*
[info] /home/apm/projects/skala-unit-tests/src/test/scala/maqi/MySpec.scala:15: inferred view from scala.collection.immutable.IndexedSeq[maqi.Test.Foo] to org.scalacheck.Gen[?] = scalacheck.this.Gen.value[scala.collection.immutable.IndexedSeq[maqi.Test.Foo]]:(x: scala.collection.immutable.IndexedSeq[maqi.Test.Foo])org.scalacheck.Gen[scala.collection.immutable.IndexedSeq[maqi.Test.Foo]]
> test
[info] Compiling 3 Scala sources to /home/apm/projects/skala-unit-tests/target/scala-2.10/test-classes...
[error] /home/apm/projects/skala-unit-tests/src/test/scala/maqi/MySpec.scala:16: overloaded method value forAll with alternatives:
[error] (genAndNameA: (org.scalacheck.Gen[maqi.Test.Foo], String),configParams: maqi.Test.sut.PropertyCheckConfigParam*)(fun: maqi.Test.Foo => Unit)(implicit config: maqi.Test.sut.PropertyCheckConfig, implicit shrA: org.scalacheck.Shrink[maqi.Test.Foo])Unit <and>
[error] (genA: org.scalacheck.Gen[maqi.Test.Foo],configParams: maqi.Test.sut.PropertyCheckConfigParam*)(fun: maqi.Test.Foo => Unit)(implicit config: maqi.Test.sut.PropertyCheckConfig, implicit shrA: org.scalacheck.Shrink[maqi.Test.Foo])Unit <and>
[error] (nameA: String,configParams: maqi.Test.sut.PropertyCheckConfigParam*)(fun: maqi.Test.Foo => Unit)(implicit config: maqi.Test.sut.PropertyCheckConfig, implicit arbA: org.scalacheck.Arbitrary[maqi.Test.Foo], implicit shrA: org.scalacheck.Shrink[maqi.Test.Foo])Unit <and>
[error] (fun: maqi.Test.Foo => Unit)(implicit config: maqi.Test.sut.PropertyCheckConfig, implicit arbA: org.scalacheck.Arbitrary[maqi.Test.Foo], implicit shrA: org.scalacheck.Shrink[maqi.Test.Foo])Unit
[error] cannot be applied to (scala.collection.immutable.IndexedSeq[maqi.Test.Foo])
[error] sut.forAll[Foo](items) { x => Console println x.i }
[error] ^
*/