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

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

参与者远程处理和集群与正在运行的应用程序(为路由测试启动)发生冲突。它们采用相同的配置,因此都尝试使用相同的端口,但会发生冲突。

在 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))
于 2016-03-17T16:56:55.897 回答