groovy / grails / 单元测试 / createCriteria.get

发布于 2024-09-19 10:59:55 字数 846 浏览 6 评论 0原文

我可以模拟调用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆萌少年 2024-09-26 10:59:55

我找到了一个不会损害我编写单元测试的能力的解决方案 -

def myCriteria = new Expando();
myCriteria .get = {Closure  cls -> returnThisObject}         
MyDomainClass.metaClass.static.createCriteria = {myCriteria }

它完全符合我的要求,并且可能支持测试提供的参数。感谢其他人的回复。希望这对其他测试域/createCriteria() 方法的人有用。

I've found a solution that doesn't compromise my ability to write unit tests -

def myCriteria = new Expando();
myCriteria .get = {Closure  cls -> returnThisObject}         
MyDomainClass.metaClass.static.createCriteria = {myCriteria }

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.

只涨不跌 2024-09-26 10:59:55

我不会打扰。相反,在您的域类中创建方法并模拟它们。这使得测试变得更容易,但更重要的是具有将持久性保持在其所属位置的优点,而不是将其分散在整个代码库中:

class MyDomainClass {
   String foo
   int bar

   static MyDomainClass findAllByIdAndAnotherParameter(long id, long anotherParameterId) {
      createCriteria().list {
         eq('id',id)
         eq('anotherParameter',anotherParameterId)
      }
   }

   static MyDomainClass getByIdAndAnotherParameter(long id, long anotherParameterId) {
      createCriteria().get {
         eq('id',id)
         eq('anotherParameter',anotherParameterId)
      }
   }
}

然后在测试中,只需将其模拟为

def testInstances = [...]
MyDomainClass.metaClass.static.findAllByIdAndAnotherParameter = { long id, long id2 ->
   return testInstances
}

def testInstance = new MyDomainClass(...)
MyDomainClass.metaClass.static.getByIdAndAnotherParameter = { long id, long id2 ->
   return testInstance
}

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:

class MyDomainClass {
   String foo
   int bar

   static MyDomainClass findAllByIdAndAnotherParameter(long id, long anotherParameterId) {
      createCriteria().list {
         eq('id',id)
         eq('anotherParameter',anotherParameterId)
      }
   }

   static MyDomainClass getByIdAndAnotherParameter(long id, long anotherParameterId) {
      createCriteria().get {
         eq('id',id)
         eq('anotherParameter',anotherParameterId)
      }
   }
}

Then in your tests, just mock it as

def testInstances = [...]
MyDomainClass.metaClass.static.findAllByIdAndAnotherParameter = { long id, long id2 ->
   return testInstances
}

and

def testInstance = new MyDomainClass(...)
MyDomainClass.metaClass.static.getByIdAndAnotherParameter = { long id, long id2 ->
   return testInstance
}
り繁华旳梦境 2024-09-26 10:59:55

现在使用 GrailsUnitTestCase.mockDomain1 方法。

grails-app/domain/sandbox/grails/foo/Something.groovy

package sandbox.grails.foo

class Something {
    String name
}

测试/unit/sandbox/grails/foo/SomethingTests.groovy

package sandbox.grails.foo

import grails.test.mixin.*
import org.junit.*

@TestFor(Something)
class SomethingTests {

    void testSomething() {

        mockDomain(Something, [
            new Something(name: 'Foo'),
            new Something(name: 'Bar'),
            new Something(name: 'Boo'),
            new Something(name: 'Baz')
        ])

        def actuals = Something.createCriteria().list(sort: 'name', order: 'asc') {
            like('name', '%oo')
        }

        assertEquals 2, actuals.size()

    }
}

This should be much simpler now with the GrailsUnitTestCase.mockDomain1 method.

grails-app/domain/sandbox/grails/foo/Something.groovy

package sandbox.grails.foo

class Something {
    String name
}

test/unit/sandbox/grails/foo/SomethingTests.groovy

package sandbox.grails.foo

import grails.test.mixin.*
import org.junit.*

@TestFor(Something)
class SomethingTests {

    void testSomething() {

        mockDomain(Something, [
            new Something(name: 'Foo'),
            new Something(name: 'Bar'),
            new Something(name: 'Boo'),
            new Something(name: 'Baz')
        ])

        def actuals = Something.createCriteria().list(sort: 'name', order: 'asc') {
            like('name', '%oo')
        }

        assertEquals 2, actuals.size()

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文