我正在玩sharing fixtures,尤其是使用混合(NoArgTest)。但是,如果引用NoArgTest,我的代码将无法编译。
我唯一的依赖:
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.0</version>
</dependency>
我的代码尝试将NoArgTest作为类型引用:
1 class Example {
2 def foo(test: org.scalatest.TestSuite) = "foo"
3 def bar(test: org.scalatest.TestSuite.NoArgTest) = "bar"
4 }
mvn编译:
[WARNING] Expected all dependencies to require Scala version: 2.11.8
[WARNING] org.scalatest:scalatest_2.11:3.0.0 requires scala version: 2.11.8
[WARNING] org.scalactic:scalactic_2.11:3.0.0 requires scala version: 2.11.8
[WARNING] org.scala-lang:scala-reflect:2.11.8 requires scala version: 2.11.8
[WARNING] org.scala-lang.modules:scala-xml_2.11:1.0.5 requires scala version: 2.11.7
[WARNING] Multiple versions of scala libraries detected!
...
[ERROR] Example.scala:4: error: object TestSuite is not a member of package org.scalatest
[ERROR] Note: trait TestSuite exists, but it has no companion object.
[ERROR] def bar(test: org.scalatest.TestSuite.NoArgTest) = "bar"
为什么?
答案 0 :(得分:0)
您可以编写TestSuite#NoArgTest
(这是独立于套件的所有NoArgTest
的常见类型),如果您已经拥有实例suite.NoArgTest
,则可以suite: TestSuite
。请注意,如果suite
是成员参数,则它们必须位于单独的参数列表中:
def bar(suite: TestSuite)(test: suite.NoArgTest) = ...
有关详细信息,请参阅What are the differences between Scala’s inner classes and Java’s Inner/nested classes?的答案。