groovy / grails / unit testing / createCriteria.get

后端 未结 3 1024
無奈伤痛
無奈伤痛 2021-02-08 11:20

I can mock calls to:

MyDomainClass.createCriteria().list{
    eq(\'id\',id)
    eq(\'anotherParameter\',anotherParameterId)
}

with:

<         


        
3条回答
  •  旧巷少年郎
    2021-02-08 11:53

    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()
    
        }
    }
    

提交回复
热议问题