What should we do?
An actor for starting our routes
Base class for use case testing
What should we do?
There are several directions we could take when testing our routes (no pun intended ).
For our purpose, we will define some use cases and then develop some more helper code, which will allow us to fire up our routes and do real HTTP requests and check the database and the responses.
Testing our routes
Testing our routes
An actor for starting our routes
We build upon our BaseSpec class and call it BaseUseCaseSpec. In it, we will do some more things, such as define a global base URL that can be used from the tests to make correct requests. Additionally, we will write a small actor, which simply starts an Akka-HTTP server.
xxxxxxxxxx
final class BaseUseCaseActor(repo: Repository, mat: ActorMaterializer)
extends Actor with ActorLogging {
import context.dispatcher
implicit val system: ActorSystem = context.system
implicit val materializer: ActorMaterializer = mat
override def receive: Receive = {
case BaseUseCaseActorCmds.Start =>
val productRoutes = new ProductRoutes(repo)
val productsRoutes = new ProductsRoutes(repo)
val routes = productRoutes.routes ~ productsRoutes.routes
val host = context.system.settings.config.getString("api.host")
val port = context.system.settings.config.getInt("api.port")
val _ = Http().bindAndHandle(routes, host, port)
case BaseUseCaseActorCmds.Stop =>
context.stop(self)
}
}
object BaseUseCaseActor {
def props(repo: Repository, mat: ActorMaterializer): Props =
Props(new BaseUseCaseActor(repo, mat))
sealed trait BaseUseCaseActorCmds
object BaseUseCaseActorCmds {
case object Start extends BaseUseCaseActorCmds
case object Stop extends BaseUseCaseActorCmds
}
}