3

次のようにルートをテストしたいScalatestRouteTest

trait MyRoutes extends Directives {

  self: Api with ExecutionContextProvider =>

  val myRoutes: Route =
    pathPrefix("api") {
      path("") {
        (get & entity(as[MyState])) {
          request => {
            complete(doSomething(request.operation))
          }
        }
      }
    }
  }
}


class RoutesSpec extends WordSpecLike with Api with ScalatestRouteTest 
  with Matchers with MyRoutes with MockitoSugar {

  "The Routes" should {

    "return status code success" in {
      Get() ~> myRoutes ~> check {
        status shouldEqual StatusCodes.Success
      }
    }
  }
}

テストを実行すると、ランタイム エラーが発生します。

テスト MyRoutesSpec を実行できませんでした: org.jboss.netty.channel.ChannelException: バインドに失敗しました: /127.0.0.1:2552

ローカルホストにバインドしたくありません。これはどのように達成できますか?

4

1 に答える 1

4

解決策は、リモーティングとクラスタリングを無効にし (これは別の構成ファイルで有効にされていました)、既定のプロバイダーを使用することでした。

アクターのリモート処理とクラスタリングが、実行中のアプリケーション (ルーティング テストのために開始) と競合しています。それらは同じ構成を選択するため、両方が競合する同じポートを使用しようとします。

次のコードがトレイトに追加され、MyRoutes機能するようになりました。

// Quick hack: use a lazy val so that actor system can "instantiate" it
// in the overridden method in ScalatestRouteTest while the constructor 
// of this class has not yet been called.
lazy val routeTestConfig =
  """
    | akka.actor.provider = "akka.actor.LocalActorRefProvider"
    | persistence.journal.plugin = "akka.persistence.journal.inmem"
  """.stripMargin

override def createActorSystem(): ActorSystem = 
  ActorSystem("RouteTest", ConfigFactory.parseString(routeTestConfig))
于 2016-03-17T16:56:55.897 に答える