问题
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