scala - How to unit test Dispatch Http in an Akka actor? -
i have akka actor follows; receives message , returns http response.
i having trouble testing interaction dispatch http, nice library seems difficult test.
class service(serviceurl:string) extends actor actorlogging { implicit val ec = context .dispatcher override def receive: receive = { case get(ids) => request(ids) } private def request(ids:seq[int]):unit = { val requesturl = buildrequesturl(ids) val request = url(requesturl).get http(request) pipeto sender() } }
one way actor:
case class get(ids:seq[int]) class service(serviceurl:string) extends actor actorlogging { implicit val ec = context .dispatcher def receive: receive = { case get(ids) => request(ids) } def request(ids:seq[int]):unit = { val requesturl = buildrequesturl(ids) val request = url(requesturl).get executerequest(request) pipeto sender() } def executerequest(req:req) = http(req) def buildrequesturl(ids:seq[int]):string = s"http://someurl.com/?ids=${ids.mkstring(",")}" }
here, i'm providing method, executerequest
execute http request , return result. method overridden in test so:
class servicetest extends testkit(actorsystem("test")) specificationlike mockito implicitsender{ trait scoping extends scope{ def mockresult:response var url:string = "" val testref = testactorref(new service(""){ override def executerequest(req:req) = { url = req.torequest.geturl() future.successful(mockresult) } }) } "a request service " should{ "execute request , return response" in new scoping{ val mockedresp = mock[response] def mockresult = mockedresp testref ! get(seq(1,2,3)) expectmsg(mockedresp) url ==== s"http://someurl.com/?ids=${urlencoder.encode("1,2,3")}" } }
it's bit crude can effective.
Comments
Post a Comment