The documentation for unit testing a Scala application http://ift.tt/1C3qOWf talks about mocking the database access using Mockito. While this method works very well to test methods that get information from the database, I'm not seeing a clear solution how to test methods that insert, update or delete data.
This is what I have setup so far:
trait UserRepository { self: HasDatabaseConfig[JdbcProfile] =>
import driver.api._
class UserTable(tag: Tag) extends Table[userModel](tag, "users") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc )
def email = column[String]("email")
def * = (id.?, email) <> (userModel.tupled, userModel.unapply _)
}
def allUsers() : Future[Seq[userModel]]
def update(user: userModel) : Future[Int]
}
class SlickUserRepository extends UserRepository with HasDatabaseConfig[JdbcProfile] {
import driver.api._
protected val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
private val users = TableQuery[UserTable]
override def allUsers(): Future[Seq[userModel]] = {
db.run(users.result)
}
def update(user: userModel): Future[Int] = {
db.run(userTableQuery.filter(_.id === user.id).update(user))
}
}
class UserService(userRepository: UserRepository) {
def getUserById(id: Int): Future[Option[userModel]] = {
userRepository.allUsers().map { users =>
users.find(_.id.get == id)
}
// TODO, test this...
def updateUser(user: userModel): Future[Int] = {
userRepository.update(user)
}
}
And then my tests:
class UserSpec extends PlaySpec with MockitoSugar with ScalaFutures {
"UserService" should {
val userRepository = mock[UserRepository]
val user1 = userModel(Option(1), "user1@test.com")
val user2 = userModel(Option(2), "user2@test.com")
// mock the access and return our own results
when(userRepository.allUsers) thenReturn Future {Seq(user1, user2)}
val userService = new UserService(userRepository)
"should find users correctly by id" in {
val future = userService.getUserById(1)
whenReady(future) { user =>
user.get mustBe user1
}
}
"should update user correctly" in {
// TODO test this
}
}
I suppose I need to mock out the 'update' method and create a stub that takes the argument and updates the mocked data. However, my skills in Scala are limited and I can't wrap my head around it. Is there perhaps a better way?
Thanks!
Aucun commentaire:
Enregistrer un commentaire