groovy / grails / 单元测试 / createCriteria.get
我可以模拟调用:
MyDomainClass.createCriteria().list{
eq('id',id)
eq('anotherParameter',anotherParameterId)
}
with:
def myCriteria = [
list : {Closure cls -> returnThisObject}
]
MyDomainClass.metaClass.static.createCriteria = { myCriteria }
按照以下建议:
http ://davistechyinfo.blogspot.com/2010/01/mocking-hibernate-criteria-in-grails.html
但是:
MyDomainClass.createCriteria().get{
eq('id',id)
eq('anotherParameter',anotherParameterId)
}
这种方法失败了 - 也许是因为“get”是一个关键字,“list”是不是。任何人都可以建议 - 能够在域类中模拟这一点应该是可能的,而不必简单地放弃使用 createCriteria().get{}
的方法的单元测试覆盖率。
非常感谢建议,
亚历克斯
I can mock calls to:
MyDomainClass.createCriteria().list{
eq('id',id)
eq('anotherParameter',anotherParameterId)
}
with:
def myCriteria = [
list : {Closure cls -> returnThisObject}
]
MyDomainClass.metaClass.static.createCriteria = { myCriteria }
as advised at:
http://davistechyinfo.blogspot.com/2010/01/mocking-hibernate-criteria-in-grails.html
but for:
MyDomainClass.createCriteria().get{
eq('id',id)
eq('anotherParameter',anotherParameterId)
}
This approach fails - maybe because 'get' is a keyword in a way 'list' is not. Can anyone advise - being able to mock this in domain classes should be possible, without simply abandoning unit test coverage for methods that use createCriteria().get{}
.
Suggestions much appreciated,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个不会损害我编写单元测试的能力的解决方案 -
它完全符合我的要求,并且可能支持测试提供的参数。感谢其他人的回复。希望这对其他测试域/createCriteria() 方法的人有用。
I've found a solution that doesn't compromise my ability to write unit tests -
which does exactly what I wanted and potentially supports testing supplied arguments. Thanks for the other response. Hope this is useful to others testing domain/createCriteria() methods.
我不会打扰。相反,在您的域类中创建方法并模拟它们。这使得测试变得更容易,但更重要的是具有将持久性保持在其所属位置的优点,而不是将其分散在整个代码库中:
然后在测试中,只需将其模拟为
和
I wouldn't bother. Instead create methods in your domain class and mock those. This makes testing easier but more importantly has the advantage of keeping persistence where it belongs instead of scattering it throughout the code base:
Then in your tests, just mock it as
and
现在使用
GrailsUnitTestCase.mockDomain
1 方法。grails-app/domain/sandbox/grails/foo/Something.groovy
测试/unit/sandbox/grails/foo/SomethingTests.groovy
This should be much simpler now with the
GrailsUnitTestCase.mockDomain
1 method.grails-app/domain/sandbox/grails/foo/Something.groovy
test/unit/sandbox/grails/foo/SomethingTests.groovy