Grails 引导集成测试
我正在尝试将一些测试数据插入到我的数据库中,由一个名为 BootStrapTest 的类来完成这项工作。
在我的 BootStrap.groovy
文件中,它的调用方式如下
environments {
test {
println "Test environment"
println "Executing BootStrapTest"
new BootStrapTest().init()
println "Finished BootStrapTest"
}
}
但是,当我运行集成测试时,此代码不会执行。我读过集成测试应该引导,所以我很困惑。
我看到了一些侵入性的解决方案,例如修改TestApp.groovy脚本,但我想有一条通过conf的途径来实现这一点。另请阅读这个SO问题和这个也是如此,但不太明白。
也许我误解了一些东西,我在 grails 测试方面遇到了很多麻烦。如果它能带来什么好处,我会使用 IntelliJ Idea 作为 IDE。
I'm trying to insert some test data into my database, for which a class called BootStrapTest does the work.
In my BootStrap.groovy
file it's called like this
environments {
test {
println "Test environment"
println "Executing BootStrapTest"
new BootStrapTest().init()
println "Finished BootStrapTest"
}
}
However, when I run my integration tests, this code doesn't execute. I've read that integration tests should bootstrap, so I'm quite confused.
I saw some invasive solutions, such as modifying the TestApp.groovy script, but I would imagine that there is a road through conf to achieve this. Also read this SO question and this one as well, but didn't quite get it.
Maybe I'm misunderstanding something, I'm having a lot of trouble with grails testing. If it brings anything to the table, I'm using IntelliJ Idea as an IDE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有引导代码都必须从 Init 闭包中调用。所以这个版本应该可以工作:
或者,您可以有一个单独的引导文件来插入测试数据,而不是调用 BootStrapTest.init()。 grails-app/conf 文件夹中名为 *BootStrap.groovy 的任何类(例如 TestBootStrap.groovy)都在引导阶段运行。请参阅http://www.grails.org/Bootstrap+Classes
All bootstrap code must be called from the Init closure. So this version should work:
Alternatively, you could have a seperate bootstrap file for inserting test data, rather than calling BootStrapTest.init(). Any class in the grails-app/conf folder which is named *BootStrap.groovy (e.g., TestBootStrap.groovy) is run in the bootstrap phase. See http://www.grails.org/Bootstrap+Classes
来自 2.0 文档:
按环境引导
通常当您的应用程序在每个环境的基础上启动时运行代码是理想的。为此,您可以使用 grails-app/conf/BootStrap.groovy 文件对每个环境执行的支持:
From the 2.0 documentation:
Per Environment Bootstrapping
Its often desirable to run code when your application starts up on a per-environment basis. To do so you can use the grails-app/conf/BootStrap.groovy file's support for per-environment execution:
在 BootStrap.groovy 中你可以尝试这样的事情
in BootStrap.groovy you can try something like this
这在 1.3.4 上适用于我:
此集成测试通过:
this works for me on 1.3.4:
this integration test passes: