我正在使用 play 2.0 和 slick。所以我为这样的模型编写单元测试。
describe("add") {
it("questions be save") {
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
// given
Questions.ddl.create
Questions.add(questionFixture)
// when
val q = Questions.findById(1)
// then
// assert!!!
}
}
}
它运行良好,但以下片段在每个单元测试中都会重复。
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
Questions.ddl.create
// test code
}
所以,我想将此代码移至块之前,如下所示。
before {
Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
Questions.ddl.create
}
}
describe("add") {
it("questions be save") {
// given
Questions.add(questionFixture)
// when
val q = Questions.findById(1)
// then
// assert!!!
}
}
}
我可以在 before 块中创建会话,然后在单元测试中使用该会话吗?
您可以使用 createSession() 并自行处理生命周期。我习惯了 JUnit,我不知道您正在使用的测试框架的具体细节,但它应该看起来像这样:
// Don't import threadLocalSession, use this instead:
implicit var session: Session = _
before {
session = Database.forURL(...).createSession()
}
// Your tests go here
after {
session.close()
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)