Grails:在测试中添加运行时可用的方法?
我正在尝试测试一个控制器,特别是使用 java.io.File 以及 Grails 在运行时注入的一些方法/属性,例如:
empty 文件项 我收到
以下错误:
No such property: empty for class: java.io.File
有些方法很容易实现:
def testFile = new File('test/unit/resources/TestScript.groovy')
File.metaClass.getFileItem = { -> testFile }
File.metaClass.isEmpty = { -> false }
但其他方法则不然,所以我想知道是否有办法让 Grails 为我添加它们?我的理解是,这是在集成测试中完成的,但是将测试移到那里会给我带来同样的错误。
任何帮助表示赞赏。
谢谢乔纳斯
I am trying to test a controller which, in particular, is using java.io.File and some of the methods/attributes injected by Grails during runtime, like:
empty
fileItem
transferTo()
I receive the following error:
No such property: empty for class: java.io.File
Some methods are easily implemented:
def testFile = new File('test/unit/resources/TestScript.groovy')
File.metaClass.getFileItem = { -> testFile }
File.metaClass.isEmpty = { -> false }
But others are not, so I wonder if there's a way to let Grails add them for me? My understanding was that this is done in integration tests but moving the test there gives me the same error.
Any help appreciated.
Thanks
Jonas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的测试类是从
GroovyTestCase
继承还是从ControllerUnitTestCase
继承?对于集成测试,它应该是
GroovyTestCase
。Does your test class inherit from
GroovyTestCase
or fromControllerUnitTestCase
?For integration tests it should be
GroovyTestCase
.确实,我在 javadocs 中也找不到它。我们的操作看起来非常类似于以下站点上使用它的清单 6:
http://www.ibm.com/developerworks/java/library/j-grails06099/index.html
有趣的是,我没有收到以下行的错误:
True, I cannot find it in the javadocs either. Our action looks pretty much like the Listing 6 on the following site which is using it:
http://www.ibm.com/developerworks/java/library/j-grails06099/index.html
And interestingly enough I do not get the error with the following line: