如何测试 grails Criteria 查询?
第一篇文章在这里,希望与很多人相关。
我正在为一个域编写集成测试,并且在该域上我有一个使用 withCriteria()
方法的查询。我在整个网络上进行了搜索,发现许多内容为您提供了有关如何模拟条件查询的详细说明,但没有提供有关如何准确测试它的说明。
我尝试使用 mockDomain(domain,list)
函数模拟域,并为测试设置一个域以在 setUp()
中使用,然后调用条件而我什么也没得到。 我在这里做了类似的 findBy
并得到了结果,但不完全是我正在寻找的结果。我很确定这不仅仅是我的查询,而是标准,我在一些地方读过标准在服务测试中不起作用。到目前为止,该查询在应用程序中对我有用,但我想要进行一些测试,以便稍后参考,以防我的代码库发生变化。
我实际上已经按照许多人的建议做了,并提取了查询的代码,并将其设为我的域中的静态方法,以便我可以模拟它以进行使用它的测试,但现在我陷入了如何实际测试的困境这部分。我是否需要运行该应用程序并从这个角度进行功能测试,或者是否有某种方法可以在 grails 单元/集成测试中执行此操作。我将在下面发布我的查询。
static Attribute getDefinitionsUsingCriteria(List categoryNames, List types){
def definitions = Definition.withCriteria() {
and {
'in'('type', types)
if (categoryNames) {
categories {
'in'('name', categoryNames)
}
}
}
}
return definitions
}
Definitions 有一个字符串属性类型,并且有一个 Set
类型的属性类别,该集合中的每个元素都有一个 String
name 属性。
我对 grails 还很陌生,并且已经阅读了很多参考书,令我惊讶的是迄今为止我读过的所有书籍中都缺少这一点。我希望这只是我的一个错误,并且很容易测试。我感谢任何帮助,并感谢您阅读这篇长文章。
JR。
first post here and hopefully relevant to many people.
I'm working on writing integration tests for a domain and on that domain I have a query using the withCriteria()
method. I've searched all over the net and found many that give you detailed instructions on how to go about mocking a criteria query, but none on how to exactly test it.
I've tried mocking the domain using the mockDomain(domain,list)
function, and setting up a domain for the test to use in the setUp()
then calling the criteria and I get nothing.
I did a similar findBy
here and got the results, but not exactly the ones I was looking for. I'm pretty sure it's not just my query, but the criteria, I've read in a few places criteria does not work in service testing. The query thus far has worked for me in the app, but I want to have some tests that I can refer to later in case my code base changes.
I've actually done as many have suggested and pulled out the code for the query and made it a static method in my domain so that I can mock it for the tests that use it, but now I'm stuck with how to actually test this part. Do I need to run the app and just do functional testing from that standpoint, or is there some way I could do this in the grails unit/integration testing. I'll post my query below.
static Attribute getDefinitionsUsingCriteria(List categoryNames, List types){
def definitions = Definition.withCriteria() {
and {
'in'('type', types)
if (categoryNames) {
categories {
'in'('name', categoryNames)
}
}
}
}
return definitions
}
Definitions has a string property type, and has a property categories of type Set
that each element in this set has a String
name property.
I'm still pretty new to grails and have been reading many reference books, and I'm surprised this is missing in all of the books I've read thus far. I hope this is something that is just a mistake on my part, and easily testable. I appreciate any help, and thanks for reading this long post.
JR.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法:将测试从
test/unit
移动到test/integration
文件夹。 Criteria 在单元测试中不起作用(那里没有 Hibernate),但在集成中会起作用。切勿在集成测试中使用mockDomain()
。注意:不要使方法静态 - 它只会使测试复杂化。
第二种方式:在单元测试中 - 使用
mockDomain()
。只需依赖逻辑非常简单的事实,并对除方法之外的所有内容进行单元测试。在setUp()
中重写它,如下所示:One way: move the test from
test/unit
totest/integration
folder. Criteria won't work in unit test (there's no Hibernate there), but will in integration. Never usemockDomain()
in integration tests.Note: don't make the method static - it only complicates testing.
Second way: In unit tests - use
mockDomain()
. Just rely on the fact that the logic is pretty straightforward, and unit-test everything except the method. Override it insetUp()
like:Grails 2.0.1 现在具有用于测试标准的原生 @Mock,但 groupProperty 尚未实现。
我编写了模拟标准插件(使用 groupProperty)
https://github.com/fabiooshiro/ Plastic-criteria
它适用于 1.3.7
Grails 2.0.1 now has native @Mock for test criteria, but groupProperty is not implemented yet.
I wrote mock criteria plugin (with groupProperty)
https://github.com/fabiooshiro/plastic-criteria
it works in 1.3.7
从 grails 2.2 开始支持标准。请参阅 Grails 2.2 中的新增功能
Criteria are supported since grails 2.2. See "Unit Testing GORM" at What's new in Grails 2.2