如何不使用 ScalatestRouteTest 绑定到本地主机

How to not bind to localhost with ScalatestRouteTest

我想用 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
      }
    }
  }
}

当运行测试时我得到运行时错误:

Could not run test MyRoutesSpec: org.jboss.netty.channel.ChannelException: Failed to bind to: /127.0.0.1:2552

我不想绑定到本地主机。如何实现?

解决方案是禁用远程处理和集群(这是在单独的配置文件中启用的)并使用默认提供程序。

actor 远程处理和集群与 运行 应用程序冲突(为路由测试启动)。他们选择了相同的配置,因此都尝试使用相同的冲突端口。

在 trait 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))