Integration Testing with Play, Akka, and Websockets

I am trying to perform some acceptance / integration tests with websockets in my application (play, akka, scalatest ...), which will be consumed by the mobile application. I did a few unit test for each actor (in the future I want this "block" to apply to several actors). Now I want to test the whole system.

For an endpoint without a websocket, for example:

package controllers

import play.api.mvc.{Action, Controller}

class StatusController extends Controller {
  def get() = Action { implicit request =>
    Ok
  }
}

I have a test:

package acceptance

import org.scalatestplus.play.{OneServerPerSuite, PlaySpec}
import play.api.libs.ws.WS
import play.api.test.Helpers._


class StatusSpec extends PlaySpec with OneServerPerSuite {
    "Server Status" should {
      "work" in {
        val response = await(WS.url(s"http://localhost:$port/status").get())

        response.status mustBe OK
      }
    }
}

In this case, everything looks fine and works :) The problem is with the controller that processes the web ports:

@Singleton
class ChatController @Inject()(system: ActorSystem) extends Controller {

  def socket() = WebSocket.tryAcceptWithActor[JsValue, JsValue] { implicit request =>
    Future.successful(request.session.get("user") match {
      case None => Left(Forbidden)
      case Some(userId) => Right(TalkerSocket.props(userId))
    })
  }
}

I was looking for some guides or tutorials to test these things without luck. I saw that you can verify this using a browser and Selenium ...

, -, . , ? ?

+3

Source: https://habr.com/ru/post/1617298/


All Articles