为什么我在 scalatest 套件中混合 TestSuiteMixIn 特征的顺序很重要?
Why does it matter what order I mix in TestSuiteMixIn traits in scalatest suites?
我创建了以下装置:
trait DatabaseFixture extends TestSuiteMixin { this: TestSuite =>
// Just setting up a test database
val cpds = new ComboPooledDataSource
val url : URL = getClass.getResource( "c3p0.properties" )
val db = Database.forDataSource(cpds, Some(50))
val users = Schema.users
val instances = Schema.instances
Await.result(db.run( DBIO.seq(
users.schema.create,
) ), 3 seconds )
abstract override def withFixture(test: NoArgTest): Outcome = {
try super.withFixture(test)
finally cpds.close()
}
}
trait UserControllerFixture extends DatabaseFixture with ScalatraSuite { this: TestSuite =>
addServlet( new UserController(db), "/user/*" )
abstract override def withFixture(test: NoArgTest): Outcome = {
super.withFixture( test )
}
}
这是我将它们混合到测试套件中的第一种方法:
class UserControllerTestSuite extends DatabaseFixture with ScalatraSuite with FlatSpecLike with Matchers {
"POST to /user/add" should "return 201 for created" in {
post( "/instance/add" ) {
status shouldBe 201
}
}
}
编译失败,出现以下错误:method withFixture in trait TestSuite of type (test: UserControllerTestSuite.this.NoArgTest)org.scalatest.Outcome has weaker access privileges; it should be public
然而,当我在 中混合 fixtures 之后 其他 scalatest 特征时,它编译得很好:
class UserControllerTestSuite extends ScalatraSuite with FlatSpecLike with Matchers with DatabaseFixture {
"POST to /user/add" should "return 201 for created" in {
post( "/instance/add" ) {
status shouldBe 201
}
}
}
这是怎么回事? withFixture() 有 "weaker access privileges" 是什么意思?
Scala 中的 Mixins 从右到左扫描。这就是为什么在您的代码有效的情况下 DatabaseFixture 在其他特征之前被调用的原因。
因此,在使用 withFixture 方法的 DatabaseFixture 之前有一些其他特征 (TestSuite) 之前,它试图覆盖它 "weaker access privilege",这正是它所说的。例如,您不能用 private 覆盖 public 方法。它必须具有相同的优先级或更高的优先级(public > 在您的情况下受保护。)
我创建了以下装置:
trait DatabaseFixture extends TestSuiteMixin { this: TestSuite =>
// Just setting up a test database
val cpds = new ComboPooledDataSource
val url : URL = getClass.getResource( "c3p0.properties" )
val db = Database.forDataSource(cpds, Some(50))
val users = Schema.users
val instances = Schema.instances
Await.result(db.run( DBIO.seq(
users.schema.create,
) ), 3 seconds )
abstract override def withFixture(test: NoArgTest): Outcome = {
try super.withFixture(test)
finally cpds.close()
}
}
trait UserControllerFixture extends DatabaseFixture with ScalatraSuite { this: TestSuite =>
addServlet( new UserController(db), "/user/*" )
abstract override def withFixture(test: NoArgTest): Outcome = {
super.withFixture( test )
}
}
这是我将它们混合到测试套件中的第一种方法:
class UserControllerTestSuite extends DatabaseFixture with ScalatraSuite with FlatSpecLike with Matchers {
"POST to /user/add" should "return 201 for created" in {
post( "/instance/add" ) {
status shouldBe 201
}
}
}
编译失败,出现以下错误:method withFixture in trait TestSuite of type (test: UserControllerTestSuite.this.NoArgTest)org.scalatest.Outcome has weaker access privileges; it should be public
然而,当我在 中混合 fixtures 之后 其他 scalatest 特征时,它编译得很好:
class UserControllerTestSuite extends ScalatraSuite with FlatSpecLike with Matchers with DatabaseFixture {
"POST to /user/add" should "return 201 for created" in {
post( "/instance/add" ) {
status shouldBe 201
}
}
}
这是怎么回事? withFixture() 有 "weaker access privileges" 是什么意思?
Scala 中的 Mixins 从右到左扫描。这就是为什么在您的代码有效的情况下 DatabaseFixture 在其他特征之前被调用的原因。
因此,在使用 withFixture 方法的 DatabaseFixture 之前有一些其他特征 (TestSuite) 之前,它试图覆盖它 "weaker access privilege",这正是它所说的。例如,您不能用 private 覆盖 public 方法。它必须具有相同的优先级或更高的优先级(public > 在您的情况下受保护。)