这是为了玩!框架 2.0。
我正在尝试编写一个简单的测试用例,以确保我的用户模型正常运行并将数据保存在我的数据库中。如果可能的话,我想在内存中运行它,这样每次新的运行我都可以重新开始。
我遇到的问题是我的进化运行(创建了表,插入了数据,但我无法查询它是否存在)。首先,我的代码。
CREATE TABLE user_data (
id SERIAL PRIMARY KEY,
user_name varchar(256) UNIQUE NOT NULL,
email varchar(256) NOT NULL,
password varchar(256) NOT NULL,
edits int NOT NULL,
reports int NOT NULL,
active BOOLEAN NOT NULL);
INSERT INTO user_data(user_name, email, password, edits, reports, active) VALUES ('user1', 'user1@email.com', '12345678', 0, 0, true);
在 application.conf
db.default.driver=org.postgresql.Driver
db.default.url="postgres://user:password@localhost:5432/ME"
在 build.scala
val appDependencies = Seq(
// Add your project dependencies here,
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
测试代码
class User_dataSpec extends Specification {
"The Database" should {
"persist data properly" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
//User_data.findAll().length must beEqualTo(1)
//Create users
User_data.create("user1", "password1", "email@test1.com") must beEqualTo(1)
User_data.create("user2", "password2", "email@test2.com") must beEqualTo(2)
User_data.create("user1", "password3", "email@test3.com") must beEqualTo(0)
//Count users
User_data.findAll().length must beEqualTo(2)
//Verify users exist
User_data.exists("user1") must beTrue
User_data.exists("user2") must beTrue
//Verify user doesn't exist
User_data.exists("user3") must beFalse
//Find users by ID
User_data.findUser(1).get.user_name must beEqualTo("user1")
User_data.findUser(2).get.user_name must beEqualTo("user2")
//Fail to find users by ID
User_data.findUser(3) must beNone
//Find users by user_name
User_data.findUser("user1").get.user_name must beEqualTo("user1")
User_data.findUser("user2").get.user_name must beEqualTo("user2")
//Fail to find users by user_name
User_data.findUser("user3") must beNone
//Authenticate users
User_data.authenticate("user1", "password1") must beTrue
User_data.authenticate("user2", "password2") must beTrue
//Fail to authenticate users
User_data.authenticate("user1", "password2") must beFalse
User_data.authenticate("user3", "passwordX") must beFalse
//Confirm the user was inserted properly
val user = User_data.findUser("user1")
user.get.user_name must beEqualTo("user1")
user.get.email must beEqualTo("email@test1.com")
user.get.password must beEqualTo("password1")
user.get.edits must beEqualTo(0)
user.get.reports must beEqualTo(0)
user.get.active must beTrue
}
}
}
}
此代码将按书面形式通过,但不应如此。如果我取消注释运行块中的第一个测试用例以测试我的 findAll() 函数的长度应该为 1,它将立即失败。但是,如果我将其更改为在我的机器上使用持久化的 PostgreSQL 数据库,它仍然会立即失败,但是当我查看 PostgreSQL 数据库时,我的 user_data 表中有单个进化应用插入,并且 play_evolutions 表有条目对于我的进化,并标记为状态 =“已应用”,最后一个问题 =“”。
任何帮助将不胜感激,谢谢。
(PS,我是第一次发帖,但会尽我所能尽快接受那些愿意提供帮助的人的答案)