rabejens
rabejens

Reputation: 8162

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

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?

Upvotes: 1

Views: 146

Answers (2)

Arcesio Arias Tabares
Arcesio Arias Tabares

Reputation: 47

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

Upvotes: 0

Chirlo
Chirlo

Reputation: 6130

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.

Upvotes: 1

Related Questions