playframework - Creating Test Scope/Context to seed data and run application, Scala Play Framework 2 -
i'm having trouble figuring out best way create easy way test of db centric application code. maybe skewed more oo languages, trying start application, seed database, pass seeded data test, clean database. trying use before , after blocks, have been running sorts of problem delayedinit , other race conditions. in docs can use setupdata , deletedata, doesn't provide opportunity pass data actual tests. there way achieved?
here sample of wrapper 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 before saving : var ${user} --- variable ${u}") user = u logger.info(s"variable user ${user}") } def deletedata(): unit ={ logger.info(s"after user -> $user") logger.info("removing user") user.remove(user) } }
then in test, love use this:
"with wrong password not allow user logged in" in new withuserdata{ logger.info(s"running test 1 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 code above won't work , give odd errors user being null, though before being run first. there way me pass around saved user object? love not have query object every test. seems lot of boiler plate , should handled in before , after.
any appreciated!
thanks,
mike
loock @ http://docs.scala-lang.org/tutorials/faq/initialization-order.html
you can use cake pattern
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 userservicecomponent usertestrepositorycomponent val userdatareal: withuserdata = new withuserdata userservicecomponent userrealrepositorycomponent }
or somthing http://docs.scala-lang.org/tutorials/tour/implicit-parameters.html
Comments
Post a Comment