Grails集成测试:失败…无法在null对象上调用addToPosts()方法
:D 我在一本书中学习了一个教程,而且我确实完全遵循了它。 然而,在我应该编写集成测试的部分,它突然说:Grails集成测试:失败…无法在null对象上调用addToPosts()方法,grails,groovy,Grails,Groovy,:D 我在一本书中学习了一个教程,而且我确实完全遵循了它。 然而,在我应该编写集成测试的部分,它突然说:无法在运行测试后立即调用null对象上的addtopost()方法。我想知道,会出什么问题……:|请帮忙!:)以下是测试代码: void testFirstPost() { def user = new User(userID: 'joemillan', password:'youaretheonly', homepage: 'www.gee
无法在运行测试后立即调用null对象上的addtopost()方法。我想知道,会出什么问题……:|请帮忙!:)以下是测试代码:
void testFirstPost() {
def user = new User(userID: 'joemillan', password:'youaretheonly',
homepage: 'www.geeee.com').save()
def post = new Post (content: 'hellloo oasdo sjdosa daodkao ')
user.addToPosts(post)
assertEquals 1, User.get(user.id).posts.size()
}
以下是用户类:
class User {
String userID
String password
String homepage
Profile profile
static hasMany=[posts:Post, tags:Tag]
static constraints = {
userID (unique: true, size: 6..20)
password (size: 6..20, validator:{password,userID-> return password !=userID.userID}) //validator = The password must not match the username.
homepage (url:true, nullable: true)
profile (nullable: true)
}
}
以下是博士后课程:
class Post {
String content
Date dateCreated
static constraints = {
content (blank:false)
}
static belongsTo = [user:User]
static hasMany = [tags:Tag]
static mapping = {
sort dateCreated: "desc"
}
}
save()
如果验证失败,则返回null,“www.geee.com”不是有效的URL。它将与“合作”http://www.geeee.com“
但您应该将创建和保存分为两个步骤,以便进行检查:
def user = new User(userID: 'joemillan', password:'youaretheonly',
homepage: 'www.geeee.com')
user.save()
assertFalse user.hasErrors()
或者,如果您确信该部件应该成功,并且只想测试其他部件,则使用failOnError,例如
def user = new User(userID: 'joemillan', password:'youaretheonly',
homepage: 'www.geeee.com').save(failOnError: true)
顺便提一下失败意味着测试失败,而错误与我的测试代码有关,对吗?def user=new user(userID:'joemillan',密码:'youaretheonly',主页:'www.geee.com')。save(failOnError:true)
有点像我在引导教程中看到的那样!!:D