How can I get rid of “no such property” when testing a class that accesses Item.constraints.xyz?

StackOverflow https://stackoverflow.com/questions/6113774

Вопрос

The following constellation in a unit test returns: No such property: title for class: myproject.Item Possible solutions: title

ItemController.groovy

def add = { 
    [itemInstance: new Item(), titleMin: Item.constraints.title.size.min() ] 
}

ItemControllerSpec.groovy

mockDomain Item
def result = controller.add()

How can I mock out that constraints line?

Note: I just want the test to run instead of failing due to that line.

Это было полезно?

Решение

Via metaClass. In setUp(), write something like: Item.metaClass.'static'.constraints = [ title: [ size: [ min: {5}, max: {30} ] ] ]

Другие советы

If your test class extends GrailUnitTestCase, call mockForConstraintsTests(Item) before the test executes. If you can't extend this class, try invoking grails.test.MockUtils.prepareForConstraintsTests(Item) before the test executes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top