gpt4 book ai didi

scala - 创建测试范围/上下文以播种数据并运行应用程序,Scala Play Framework 2

转载 作者:行者123 更新时间:2023-12-02 03:24:13 25 4
gpt4 key购买 nike

我在找出最佳方法时遇到了一些麻烦,无法创建一种简单的方法来测试我的一些以数据库为中心的应用程序代码。也许我不喜欢更多的 OO 语言,但我想做的是启动应用程序,为数据库播种,将播​​种数据传递给测试,然后清理数据库。我试图在 block 之前和之后使用,但一直遇到 delayedInit 和其他竞争条件的各种问题。在文档中,您可以使用 setupData 和 deleteData,但它不提供将数据传递给实际测试的机会。有什么办法可以实现吗?

这是我正在使用的包装器示例:

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)
}
}

然后在我的测试中,我喜欢这样使用它:

"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)
}

上面的代码不会工作,并且会给出关于用户为空的奇怪错误,即使 before 首先运行。我有什么办法可以传递已保存的用户对象吗?我希望不必在每次测试时都查询对象。似乎有很多样板,应该在前后处理。

如有任何帮助,我们将不胜感激!

谢谢,

迈克

最佳答案

查看http://docs.scala-lang.org/tutorials/FAQ/initialization-order.html

你可以用蛋糕图案之类的东西

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
}

或者类似 http://docs.scala-lang.org/tutorials/tour/implicit-parameters.html 的东西

关于scala - 创建测试范围/上下文以播种数据并运行应用程序,Scala Play Framework 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31304940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com