我想用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
我不想绑定到localhost。如何实现这一目标?
答案 0 :(得分:2)
解决方案是禁用远程处理和群集(这是在单独的配置文件中启用的)并使用默认提供程序。
演员远程处理和集群与正在运行的应用程序冲突(为路由测试启动)。他们选择相同的配置,因此两者都试图使用冲突的相同端口。
在特征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))