expert
expert

Reputation: 30135

How do I create mock if my trait uses self-type restriction?

I'm trying to use cake pattern and mocking. After reading bunch of blog-posts with tons of fashionable terms I can't make it working :(

I'm using Spray and have following http service

trait ApiServiceAbstract extends HttpService with SprayJsonSupport {
  this : AppProvider =>
// tons of routes here
}

where AppProvider is

trait AppProvider {
  val api : ApiManager
}

For real server I combine it like this

class ApiServiceActor extends Actor with ApiServiceAbstract with RealApiManager {
}

where RealApiManager extends AppProvider.

Also ApiManager must be mixed in with DbProvider which looks like this

trait ApiManager {
  this : DbProvider =>
}

trait RealDbProvider extends DbProvider {
  override lazy val dbManager = new DBManager
}

In my unit-test I only want to test http portion and have mocked ApiManager which simply checks that object passed by the client is correct one.

I cannot do this

override lazy val api = mock[ApiManager]

because ApiManager has to be mixed in with DbProvider. How can I fix this ? I see two options right now:

Upvotes: 1

Views: 1124

Answers (1)

Sebastien Lorber
Sebastien Lorber

Reputation: 92150

I'm not sure of my answer because you don't provide all the source code (ApiManager...)

You could create an intermediary trait.

trait ApiManagerDefaultMixin extends ApiManager with DbProvider

and then

val apiManager = new ApiManagerDefaultMixin { 
    override lazy val api = mock[ApiManager]
}

Upvotes: 1

Related Questions