When testing Spray services with Scalatest, how to introduce implicit values?

只愿长相守 提交于 2019-12-24 15:21:48

问题


I have a service:

trait MyService extends HttpService {

  def getDao(implicit dao: SomeDAO) = dao

  def someRoute = path("foo") {
    get {
      complete(getDao getSomething)
    }
  }
}

Then, I have an actor:

class MyActor extends MyService with Actor {

  override def receive: Receive = runRoute(someRoute)

  def actorRefFactory: ActorRefFactory = context
}

My test class looks like this:

class MyServiceTest extends FlatSpec with ScalatestRouteTest with MyService with Matchers with MockFactory {

  override implicit def actorRefFactory: ActorSystem = system

  implicit val _dao: SomeDAO = mock[SomeDAO]

  "My service" should "return something" in {

    Get("/foo") ~> someRoute ~> check {
      status should be(OK)
    }
  }
}

But when I run the test, the compiler complains that the implicit value for SomeDAO cannot be found. How do I manage to get the SomeDAO into my service? What am I missing / what am I doing wrong?


回答1:


I think you're better off declaring the implicit into someRoute, like this:

trait MyService extends HttpService {

def someRoute(implicit dao: SomeDAO) = path("foo") {
   get {
     complete(dao getSomething)
   }
 }
}

It should compile and it also makes more sense that having a method just to retrieve an implicit.




回答2:


The ScalatestRouteTest already provides an implicit ActorySystem. Remove the "implicit" modifier from your actorRefFactory method and the test should get executed.

this solve this problem for my code



来源:https://stackoverflow.com/questions/29827761/when-testing-spray-services-with-scalatest-how-to-introduce-implicit-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!