Grails 2.1 Unit Testing Command Object mockForConstraintsTests not working?

前端 未结 3 398
逝去的感伤
逝去的感伤 2021-01-20 04:45

I have used manually written as well as Grails generated Unit tests for this command object:

   package myapp

    @grails.validation.Validateable
    class          


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 05:02

    You are using the validate method incorrectly. You never set the field on the class, so the field is null, not blank. Try changing your test as follows:

    void testSomething() {
        SearchCommand commandUnderTest = new SearchCommand()
        commandUnderTest.basisBuild = ""
    
        assertFalse commandUnderTest.validate()
        assertEquals 'blank', commandUnderTest.errors['basisBuild']
    }
    

    Edit: There is also a grails bug when testing command classes that use the @Validatable annotation. There are some workarounds in the bug commentary.

提交回复
热议问题