Validation 在每个场景中表现不同

Validation 在每个场景中表现不同,validation,grails,groovy,gorm,Validation,Grails,Groovy,Gorm,场景1: 我有两门课: void testFirstPost() { def user = new User(userId: 'joe',password: 'secret').save() def post1 = new Post(content: "First post... W00t!") user.addToPosts(post1) def post2 = new Post(content: "Second post...") user.addToPosts(post2) } 发布:

场景1: 我有两门课:

void testFirstPost() {
def user = new User(userId: 'joe',password: 'secret').save()
def post1 = new Post(content: "First post... W00t!")
user.addToPosts(post1)
def post2 = new Post(content: "Second post...")
user.addToPosts(post2) }
发布

class Post {
String content
Date dateCreated
static constraints = {
content(blank: false)
}
static belongsTo = [ user : User ]
}
class User {
// existing code here
static hasMany = [ posts : Post ]
}
class User {
    String login
    String password
    Profile profile
    Status status
    static hasMany = [ holidays : Holiday ]
    static constraints = { 
        login(unique:true,size:2..20)
        password(size:2..20) //more to be added later!
        profile(nullable:true)
        company(nullable:true)
    }
    static belongsTo = [ company : Company ]
}
class Company {
    String shortName;
    String name 
    Date dateCreated
    String region
    String email
    Address address
    Status status   
    Long tel
    Long fax
    static hasMany = [ users : User]
        static constraints = {
        }
    static mapping = {
        address lazy:false
        status lazy:false
    }
}
用户

class Post {
String content
Date dateCreated
static constraints = {
content(blank: false)
}
static belongsTo = [ user : User ]
}
class User {
// existing code here
static hasMany = [ posts : Post ]
}
class User {
    String login
    String password
    Profile profile
    Status status
    static hasMany = [ holidays : Holiday ]
    static constraints = { 
        login(unique:true,size:2..20)
        password(size:2..20) //more to be added later!
        profile(nullable:true)
        company(nullable:true)
    }
    static belongsTo = [ company : Company ]
}
class Company {
    String shortName;
    String name 
    Date dateCreated
    String region
    String email
    Address address
    Status status   
    Long tel
    Long fax
    static hasMany = [ users : User]
        static constraints = {
        }
    static mapping = {
        address lazy:false
        status lazy:false
    }
}
这是一个经典的一对多关系。这些类的集成测试:

void testFirstPost() {
def user = new User(userId: 'joe',password: 'secret').save()
def post1 = new Post(content: "First post... W00t!")
user.addToPosts(post1)
def post2 = new Post(content: "Second post...")
user.addToPosts(post2) }
效果很好。但是如果你考虑其他两个类:

场景2:

用户

class Post {
String content
Date dateCreated
static constraints = {
content(blank: false)
}
static belongsTo = [ user : User ]
}
class User {
// existing code here
static hasMany = [ posts : Post ]
}
class User {
    String login
    String password
    Profile profile
    Status status
    static hasMany = [ holidays : Holiday ]
    static constraints = { 
        login(unique:true,size:2..20)
        password(size:2..20) //more to be added later!
        profile(nullable:true)
        company(nullable:true)
    }
    static belongsTo = [ company : Company ]
}
class Company {
    String shortName;
    String name 
    Date dateCreated
    String region
    String email
    Address address
    Status status   
    Long tel
    Long fax
    static hasMany = [ users : User]
        static constraints = {
        }
    static mapping = {
        address lazy:false
        status lazy:false
    }
}
公司

class Post {
String content
Date dateCreated
static constraints = {
content(blank: false)
}
static belongsTo = [ user : User ]
}
class User {
// existing code here
static hasMany = [ posts : Post ]
}
class User {
    String login
    String password
    Profile profile
    Status status
    static hasMany = [ holidays : Holiday ]
    static constraints = { 
        login(unique:true,size:2..20)
        password(size:2..20) //more to be added later!
        profile(nullable:true)
        company(nullable:true)
    }
    static belongsTo = [ company : Company ]
}
class Company {
    String shortName;
    String name 
    Date dateCreated
    String region
    String email
    Address address
    Status status   
    Long tel
    Long fax
    static hasMany = [ users : User]
        static constraints = {
        }
    static mapping = {
        address lazy:false
        status lazy:false
    }
}
又是经典的一对多关系

对于这些类,我编写了如下测试:

void testSaveUser() {
        def status1 = new Status(name:"Busy")
        status1.save(flush:true)        
        def user = new User(login:"anto",password:"anything",
            status:status1)
        assert user.save(flush:true, failOnError: true)
    }
即使这个测试也很好。注意,在第二个
User
类中,我将
company
字段的约束设置为
company(nullable:true)
,如果我没有设置该约束,则上述测试(
testSaveUser()
)将失败

我得到的错误是:

Validation Error(s) occurred during save(): - Field error in object 'mnm.User' on field 'company': rejected value [null]; codes [mnm.User.company.nullable.error.mnm.User.company,mnm.User.company.nullable.error.company,mnm.User.company.nullable.error.mnm.Company,mnm.User.company.nullable.error,user.company.nullable.error.mnm.User.company,user.company.nullable.error.company,user.company.nullable.error.mnm.Company,user.company.nullable.error,mnm.User.company.nullable.mnm.User.company,mnm.User.company.nullable.company,mnm.User.company.nullable.mnm.Company,mnm.User.company.nullable,user.company.nullable.mnm.User.company,user.company.nullable.company,user.company.nullable.mnm.Company,user.company.nullable,nullable.mnm.User.company,nullable.company,nullable.mnm.Company,nullable]; arguments [company,class mnm.User]; default message [Property [{0}] of class [{1}] cannot be null]
grails.validation.ValidationException: Validation Error(s) occurred during save():
- Field error in object 'mnm.User' on field 'company': rejected value [null]; codes [mnm.User.company.nullable.error.mnm.User.company,mnm.User.company.nullable.error.company,mnm.User.company.nullable.error.mnm.Company,mnm.User.company.nullable.error,user.company.nullable.error.mnm.User.company,user.company.nullable.error.company,user.company.nullable.error.mnm.Company,user.company.nullable.error,mnm.User.company.nullable.mnm.User.company,mnm.User.company.nullable.company,mnm.User.company.nullable.mnm.Company,mnm.User.company.nullable,user.company.nullable.mnm.User.company,user.company.nullable.company,user.company.nullable.mnm.Company,user.company.nullable,nullable.mnm.User.company,nullable.company,nullable.mnm.Company,nullable]; arguments [company,class mnm.User]; default message [Property [{0}] of class [{1}] cannot be null]

    at mnm.UserIntegrationTests.testSaveUser(UserIntegrationTests.groovy:18)
即使场景1场景2都是相同的,为什么场景2强制我添加
公司(可空:true)
约束

(请注意,我使用的是grails1.3.7,并在同一版本中运行了这两个场景!)


提前感谢。

如果某个
内容属于其他内容,默认情况下它不能为
null


在您的测试中,在
保存
之前,您没有(仍然)为用户设置
公司
,因此它将在默认约束下失败

如果某项
属于某项
其他内容,默认情况下它不能为


在你的测试中,在你保存之前,你没有(仍然)为用户设置一个
company
,因此它将失败,默认约束是

Oh ya,我再一次忘记了这个规则。现在它可以工作了谢谢tim_yates先生:)哦,是的,我又一次忘记了这个规则。现在它可以工作了谢谢tim_yates先生:)