Scala 使用akka http的web套接字单元测试用例

Scala 使用akka http的web套接字单元测试用例,scala,unit-testing,websocket,akka,akka-http,Scala,Unit Testing,Websocket,Akka,Akka Http,我已经使用Akka HTTP实现了web套接字。我正在使用Kafka的数据,并使用web套接字发送通知。功能运行正常,但我被困在测试用例中 ` private def loginNotificationRoute(): Route = { cors() { pathPrefix("notifications" / Segment) { userName => get { handleWebSocketMessages(notific

我已经使用Akka HTTP实现了web套接字。我正在使用Kafka的数据,并使用web套接字发送通知。功能运行正常,但我被困在测试用例中

 ` private def loginNotificationRoute(): Route = {
    cors() {
      pathPrefix("notifications" / Segment) { userName =>
        get {
          handleWebSocketMessages(notificationClientFlow(userName))
        }
      }
    }
  }`
NotificationActor从数据库读取通知并将数据发送回客户端

`  def notificationClientFlow(userName: String): Flow[Message, Message, NotUsed] = {
    info(s"Connection Request accepted for user $userName")
    val notificationActor = actorSystem.actorOf(NotificationActor.props(userName, jsonHelper))

    val incomingMessages: Sink[Message, NotUsed] =
      Flow[Message].map {
        case TextMessage.Strict(text) => NotificationActor.IncomingMessage(text)
      }.to(Sink.actorRef(notificationActor, PoisonPill))

    val outgoingMessages: Source[Message, NotUsed] =
      Source
        .actorRef[NotificationActor.ResponseType](BUFFER_SIZE, OverflowStrategy.fail)
        .mapMaterializedValue { outgoingActor =>
          notificationActor ! NotificationActor.Connected(outgoingActor)
          NotUsed
        }
        .map {
          notificationResponse: NotificationActor.ResponseType =>
            info(s"sending notification ${notificationResponse.action}")
            TextMessage.Strict(jsonHelper.write(notificationResponse))
        }

    Flow.fromSinkAndSource(incomingMessages, outgoingMessages)
  }`

重新设计依赖项

在我看来,您当前设计的最大缺点是存在对特定
actorSystem
的固有依赖性;令人不快的代码行被删除了

//actorSystem comes from the outside world!!!
val notificationActor = actorSystem.actorOf(NotificationActor.props(userName, jsonHelper))
应重新参数化
notificationClientFlow
方法,以便在参数中明确列出所有依赖项。而且不应该依赖于整个
ActorSystem
,而应该将其简化为一个
ActorRef

def notificationClientFlow(userName: String, 
                           notificationActor : ActorRef): Flow[Message, Message, NotUsed] = {
  //notificationActor is now passed in
  //val notificationActor = actorSystem.actorOf(NotificationActor.props(userName, jsonHelper))
}
这需要在
路由
创建方法中进行类似的显式声明:

private def loginNotificationRoute(notificationActor : ActorRef)() : Route = {
  ...
  handleWebSocketMessages(notificationClientFlow(userName, notificationActor))
}
测试

现在可以使用以下方法实现测试:

此外,由于我们已经明确通过了
ActorRef
,而不仅仅是
ActorSystem
,因此我们可以使用其他高级测试功能,如:

上述技术同样适用于使用以下各项测试路线:


实际问题是什么?如何编写loginNotificationRoute()和notificationClientFlow(用户名:String)的单元测试用例?
class MySpec() extends TestKit(ActorSystem("MySpec")) {

  val userName = "testUser"

  val notificationActor = 
    system.actorOf(NotificationActor.props(userName, jsonHelper))

  val testFlow = notificationClientFlow(userName, notificationActor)

}
val probe = TestProbe()

val testProbeFlow = notificationClientFlow(userName, probe.ref)
val testRoute = loginNotificationRoute(testProbe)

Get() ~> testRoute() ~> check {
  //testing assertions here
}