2017-06-13 6 views
1

書面の時点で、Play 2.6はリリース候補の状態です。非推奨の警告が発生Play 2.6でテストサーバをどのようにユニット化するかその動作シングルトンは廃止されました

Server.withRouter() { 
    case GET(p"/repositories") => Action { 
    Results.Ok(Json.arr(Json.obj("full_name" -> "octocat/Hello-World"))) 
    } 
} { implicit port => ... 
:そうようなルーティングモックサーバ用DSLを使用して

https://www.playframework.com/documentation/2.6.0-RC2/ScalaTestingWebServiceClients

即ち: Actionシングルトンは、このように、ここでのテストに関するすべての情報が廃止され、廃止されました。

これを回避する方法はありますか、またはテスト用のDSLを更新するのを待つだけですか?

答えて

0

はい、Playフレームワーク2.6でScalaTestでこれを行う新しい方法があります。 Applicationを作成して独自のRouterProviderを埋め込むには、Guiceを使用する必要があります。この例を考えてみましょう:

class MyServiceSpec 
    extends PlaySpec 
    with GuiceOneServerPerTest { 

    private implicit val httpPort = new play.api.http.Port(port) 

    override def newAppForTest(testData: TestData): Application = 
    GuiceApplicationBuilder() 
     .in(Mode.Test) 
     .overrides(bind[Router].toProvider[RouterProvider]) 
     .build() 

    def withWsClient[T](block: WSClient => T): T = 
    WsTestClient.withClient { client => 
     block(client) 
    } 

    "MyService" must { 

    "do stuff with an external service" in { 
     withWsClient { client => 
     // Create an instance of your client class and pass the WS client 
     val result = Await.result(client.getRepositories, 10.seconds) 
     result mustEqual List("octocat/Hello-World") 
     } 
    } 
    } 
} 

class RouterProvider @Inject()(action: DefaultActionBuilder) extends Provider[Router] { 
    override def get: Router = Router.from { 
    case GET(p"/repositories") => action { 
     Results.Ok(Json.arr(Json.obj("full_name" -> "octocat/Hello-World"))) 
    } 
    } 
} 
関連する問題