gpt4 book ai didi

scala - 如何在测试前启动 Play 应用程序,然后在 specs2 中将其关闭?

转载 作者:行者123 更新时间:2023-12-04 23:20:46 24 4
gpt4 key购买 nike

我的目标是让应用程序运行并在应用程序的同一实例上执行多个测试。

我试过 this solution没有太多运气。这是我的测试:

class ApplicationSpec extends Specification { sequential


object AppWithTestDb2 extends FakeApplication(additionalConfiguration =
Map(
("db.default.driver") -> "org.h2.Driver",
("db.default.url") -> (
// "jdbc:h2:mem:play-test-" + scala.util.Random.nextInt + // in memory database
"jdbc:h2:/tmp/play-test-" + scala.util.Random.nextInt + // file based db that can be accessed using h2-browse in activator
";MODE=PostgreSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1")
))

running(AppWithTestDb2) {

"Application" should {

"send 404 on a bad request" in {
route(FakeRequest(GET, "/boum")) must beNone
}

"post signup request" in {
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
status(res) must equalTo(OK)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}

"fail signup request for existing user" in {
val res1 = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res1, Duration.Inf)
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res, Duration.Inf)
status(res) must equalTo(CONFLICT)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}

}
}

}

这里的问题是应用程序会立即启动和停止,并且在没有运行应用程序的情况下执行测试:
[debug] c.j.b.BoneCPDataSource - JDBC URL = jdbc:h2:/tmp/play-test--437407884;MODE=PostgreSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1, Username = zalando, partitions = 1, max (per partition) = 30, min (per partition) = 5, idle max age = 10 min, idle test period = 1 min, strategy = DEFAULT
[info] application - Application has started
[debug] application - Binding to Slick DAO implementations.
[info] application - Application shutdown...
[debug] c.j.b.BoneCPDataSource - Connection pool has been shut down
[info] ApplicationSpec
[info]
[info] Application should
[info] ! send 404 on a bad request
[error] RuntimeException: : There is no started application (Play.scala:71)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:71)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:71)
[error] play.api.Play$.current(Play.scala:71)
[error] play.api.test.RouteInvokers$class.route(Helpers.scala:305)
[error] play.api.test.Helpers$.route(Helpers.scala:403)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8$$anonfun$apply$9.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8$$anonfun$apply$9.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8.apply(ApplicationSpec.scala:76)

最佳答案

这是我的工作解决方案

object AppWithTestDb2 extends FakeApplication(additionalConfiguration = 
Map(
("db.default.driver") -> "org.h2.Driver",
("db.default.url") -> (
// "jdbc:h2:mem:play-test-" + scala.util.Random.nextInt + // in memory database
"jdbc:h2:/tmp/play-test-" + scala.util.Random.nextInt + // file based db that can be accessed using h2-browse in activator
";MODE=PostgreSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1")
))


class SignupLoginSpec extends Specification { sequential


step(Play.start(AppWithTestDb2))

"application" should {

"post signup request" in {
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
status(res) must equalTo(OK)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}

"fail signup request for existing user" in {
val res1 = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res1, Duration.Inf)
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res, Duration.Inf)
status(res) must equalTo(CONFLICT)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}

}

step(Play.stop())

}

关于scala - 如何在测试前启动 Play 应用程序,然后在 specs2 中将其关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27567512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com