Skip to content

Instantly share code, notes, and snippets.

@iref
Last active August 29, 2015 14:09
Show Gist options
  • Save iref/2b6586b5bb9d5400471b to your computer and use it in GitHub Desktop.
Save iref/2b6586b5bb9d5400471b to your computer and use it in GitHub Desktop.
Implicits session is not found in passed anonymous function
package models
import org.specs2.mutable.Specification
import play.api.Play.current
import play.api.db.slick.{DB, Session}
import play.api.db.slick.Config.driver.simple._
import play.api.test.FakeApplication
import play.api.test.Helpers._
class UserSpec extends Specification {
"UserRepository" should {
"save new user" in {
withDatabase {
val user = User("Octocat", "[email protected]")
val savedUser = Users.save(user)
val foundUser = Users.find(savedUser.id.get).get
foundUser.name must beEqualTo("Octocat")
foundUser.email must beEqualTo("[email protected]")
}
}
}
def withDatabase[T](test: => T): T =
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
DB.withSession { implicit session: Session =>
test
}
}
}
@iref
Copy link
Author

iref commented Nov 14, 2014

Scala can't resolve implicit session for Users.save if its in passed block.
Compilation error is

could not find implicit value for parameter session: play.api.db.slick.Config.driver.simple.Session
[error]       val savedUser = Users.save(user)

@iref
Copy link
Author

iref commented Nov 14, 2014

If I wrap test directly in DB.withSession, e.g.

"save new user" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
    DB.withSession { implicit session: Session =>
      val user = User("Octocat", "[email protected]")
      val savedUser = Users.save(user)
      val foundUser = Users.find(savedUser.id.get).get
      foundUser.name must beEqualTo("Octocat")
      foundUser.email must beEqualTo("[email protected]")
    }
  }
}

Spec file is compiled without any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment