Create Test Scope / Context to Load Data and Run Application, Scala Play Framework 2

I am having trouble finding the best way to create an easy way to test some of my basic programming code. I may have been skewed from more OO languages, but what I am trying to do is start the application, seed the database, pass the seed data to the test, and then clear the database. I tried to use before and after blocks but ran into all problems with delayedInit and other race conditions. In the docs, you can use setupData and deleteData, but that doesn't give you the ability to pass the data into the actual tests. Can this be done?

Here's an example of a wrapper I'm using:

abstract class WithUserData extends WithApplication {

    var user = null

    override def around[T: AsResult](t: => T): Result = super.around {
      setupData()
      t
      deleteData()
    }

    def setupData() {
      Logger.info("Running Before")
      val passwordInfo = PasswordInfo("bcrypt", "$2a$10$at8N/GZHKDbHLh6er.UsbOUjVqx.IGebO2Wc7EmmD2m4tOlin7EAG")
      val u = User(new IdentityId("harrypotter@naytev.com", UsernamePasswordProvider.UsernamePassword), "Harry",
        "Potter", "Harry Potter", Option("harrypotter@naytev.com"), None, AuthenticationMethod.UserPassword,
        None, None, Some(passwordInfo), None, None, "", Subscription.free, None, None, None, List(), None)

      User.save(u)
      Logger.info(s"Before User is before saving : var ${user} --- variable ${u}")
      user = u
      Logger.info(s"variable user is ${user}")
    }
    def deleteData(): Unit ={
      Logger.info(s"After User is ->  $user")
      Logger.info("Removing the user")
      User.remove(user)
    }
}

      

Then in my test I would like to use it like this:

"with wrong password will not allow user to be logged in" in new WithUserData{
    Logger.info(s"Running test 1 with User ${user}")
    val fakeRequest = FakeRequest(POST, "/authenticate/userpass", FakeHeaders(), "").withFormUrlEncodedBody(("email" , user.email.get), ("password", "Blah"))

    val request = route(fakeRequest).get

    status(request) must equalTo(BAD_REQUEST)
}

      

The above code will not work and will give odd errors stating that the user is null, even though it runs earlier. Is there a way to get around the saved custom object? I would not want every object to request an object. It looks like a lot of boiler stove and needs to be something treated before and after.

Any help would be greatly appreciated!

Thank,

Mike

+3


source to share


1 answer


Loock at http://docs.scala-lang.org/tutorials/FAQ/initialization-order.html

You can use something like cake template



case class User(id: BigInt, name: String)

trait UserRepositoryComponent {
  def userLocator: UserLocator
  trait UserLocator {
    def getUser: User
    def removeUser(user: User)
  }
}

trait UserTestRepositoryComponent extends UserRepositoryComponent {
  def userLocator = new  UserTestLocator

  class UserTestLocator extends UserLocator {
    override def getUser: User = User(1, "test user")

    override def removeUser(user: User): Unit = ()
  }
}

trait UserRealRepositoryComponent extends UserRepositoryComponent {
  def userLocator = new UserRealLocator
  class UserRealLocator extends UserLocator {
    override def getUser: User = User(1, "real user")

    override def removeUser(user: User): Unit = ()
  }
}

trait UserServiceComponent {
  this: UserRepositoryComponent =>
  def getUser: User = userLocator.getUser
  def removeUser(user: User) = userLocator.removeUser(user)
}

trait WithUserData {
  this: UserServiceComponent =>
  println(getUser)
}


object Main extends App {
  val userDataFake: WithUserData = new WithUserData with UserServiceComponent with UserTestRepositoryComponent
  val userDataReal: WithUserData = new WithUserData with UserServiceComponent with UserRealRepositoryComponent
}

      

Or something like http://docs.scala-lang.org/tutorials/tour/implicit-parameters.html

+1


source







All Articles