2017-10-13 58 views
1

我有一個測試域驗證問題。 這裏是我的單元測試:Grails - 表格ID默認不可爲空或是?

@Unroll 
    void "Validate domain object SecUser when id:#id, username:#username, 
password:#password"() { 
     when: 
     SecUser user = new SecUser(id: id, username: username, password: 
password, enabled: "true", accountExpired: "false", accountLocked: 
"false", passwordExpired: "false") 

    def userSave = user.validate() 

    then: 
    assert userSave == expected 

    where: 
    id  |   username   | password ||  expected 
    527  |  "[email protected]"  | "test"  ||  true 
    null |  "[email protected]"  | "test"  ||  false 
    528  |   null   | "test"  ||  false 
    529  |  "[email protected]"  | null  ||  true 
    ''  |  "[email protected]"  | "test"  ||  false 
    530  |   ''    | "test"  ||  false 
    555  |  "[email protected]"  |  ''  ||  true 
    557  | "[email protected], [email protected]" | "test"  ||  false 

} 

的問題是,驗證結果是真實的,即使當ID爲空或「」。我沒有想到。 這是SecUser域:

class SecUser { 

Long id 
String username 
String password 
boolean enabled 
boolean accountExpired 
boolean accountLocked 
boolean passwordExpired 
String sifpartnera 
Long dodadresaid 
String sifosobe 
Long coid 
String langid 


static constraints = { 
    id(blank: false) 
    username blank: false, unique: true, email: true 
    password nullable: true 
    enabled() 
    accountExpired() 
    accountLocked() 
    passwordExpired() 
    sifpartnera nullable: true 
    dodadresaid nullable: true 
    sifosobe nullable: true 
    coid nullable: true 
    langid nullable: true 
} 

static mapping = { 
    version false 
    table name: "sec_user", schema: "public" 
    password column: '`password`' 
    id generator: 'sequence', column: 'id' 
} 
} 

我不明白有什麼問題。難道我做錯了什麼? 謝謝。

回答

4

默認情況下,id未在validate()方法中選中。其背後的原因很簡單:在實例化的那一刻,id爲空,並且僅在調用save()後纔得到設置。

相關問題