Akka Http版本:“10.0.11”
我有以下测试路线:
private def getAll: Route = pathPrefix("_all") {
get {
complete((todoRegistryActor ? GetAllTodos).mapTo[Todos].map(todosToTodoDtos))
}
}
我有以下测试:
class TodoRouteSpec extends WordSpec with Matchers
with ScalatestRouteTest with RouteManager with BeforeAndAfterAll with TestKitBase with ImplicitSender {
override implicit val system: ActorSystem = ActorSystem("TodoRouteSpec")
override val executionContext: ExecutionContext = system.dispatcher
private val todoRegistryProbe = TestProbe()
override implicit val todoRegistryActor: ActorRef = todoRegistryProbe.ref
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"The service" should {
"return a list of todos for GET _all request" in {
Get("/api/todo/_all").~>(todoRoute)(TildeArrow.injectIntoRoute).~>(check {
//todoRegistryProbe.expectMsg(GetAllTodos)
responseAs[TodosDto] shouldEqual TodosDto(Seq.empty)
status should ===(StatusCodes.OK)
})
}
}
}
运行以下测试时,收到错误: 导致中止运行的异常或错误:必须定义ActorRefFactory上下文 java.lang.IllegalArgumentException:必须定义ActorRefFactory上下文
提前致谢
解决方案
class TodoRouteSpec extends WordSpec with Matchers with ScalatestRouteTest with TodoRoute {
private lazy val routes = todoRoute
private val todoRegistryProbe = TestProbe()
todoRegistryProbe.setAutoPilot((sender: ActorRef, _: Any) => {
sender ! Todos(Seq.empty)
TestActor.KeepRunning
})
override implicit val todoRegistryActor: ActorRef = todoRegistryProbe.ref
"TodoRoute" should {
"return a list of todos for GET /todo/_all request" in {
Get("/todo/_all").~>(routes)(TildeArrow.injectIntoRoute).~>(check {
todoRegistryProbe.expectMsg(GetAllTodos)
status should ===(StatusCodes.OK)
contentType should ===(ContentTypes.`application/json`)
entityAs[TodosDto] shouldEqual TodosDto(Seq.empty)
})
}
}
}
答案 0 :(得分:1)
你不需要演员系统来进行路线测试。
请参阅documentation中的示例。
class FullTestKitExampleSpec extends WordSpec with Matchers with ScalatestRouteTest {
val smallRoute =
get {
path("ping") {
complete("PONG!")
}
}
"The service" should {
"return a 'PONG!' response for GET requests to /ping" in {
// tests:
Get("/ping") ~> smallRoute ~> check {
responseAs[String] shouldEqual "PONG!"
}
}
}
}