How can I get rid of “no such property” when testing a class that accesses Item.constraints.xyz?
-
27-12-2020 - |
Question
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.
Solution
Via metaClass. In setUp()
, write something like:
Item.metaClass.'static'.constraints = [ title: [ size: [ min: {5}, max: {30} ] ] ]
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow