gpt4 book ai didi

grails - Spock测试用例与Grails中的域类没有关系吗?

转载 作者:行者123 更新时间:2023-12-02 15:36:10 24 4
gpt4 key购买 nike

我有一个Spock测试用例,其中的setup块如下所示:

setup: "set the required objects"
def company = new Company(shortName:"infyyy",fullName:"infoysys",
region:"tamilnadu" ,email:"a@ac.com" ,telphone:34343433,fax:34343433).save(failOnError:true)
def project = new Project(name:"testing")
def userInstance = new User(username:username,password:password,
company:company,project:project,pt:["dummy"]).save(failOnError:true)
def tasksInstance = new Tasks(title:"testingwork",startDate:(new Date()-4),endDate:(new Date().clearTime()-6),description:"blah blah",project:project,completed:true,user:userInstance).save(failOnError:true)

而且, Tasks域类看起来像这样:
class Tasks {
static belongsTo = [ user : User, project: Project ]
//other code
}
User类是这样的:
class User  {
static hasMany = [ holidays : Holiday, tasks : Tasks, pt:String, project: Project ]
//other code
}

但是,当我运行测试并且测试失败时(没有错误消息,但是在我的Spock测试的 then块中失败了),我发现了一个错误。我的设置没有在 UserTasks之间创建任何关系,这使我的测试失败了。

我要测试的 Controller 代码是:
def todaysTasks() {
def user = User.get(springSecurityService.principal.id)
def choice = params.managersProject
params.max = Math.min(params.max ? params.int('max') : 10,100)
def search = Tasks.createCriteria().list(max: params.max as Integer, offset: params.offset as Integer, order: params.order as String, sort : params.sort) {
and {
project {
like('name',"${choice}")
}
eq('endDate', new Date().clearTime())
}
}
println "todays task selected project is " + search
[tasksInstanceList : search, tasksInstanceTotal: search.getTotalCount() ]
}

上面测试中的 println打印 0。即使我将测试中的 endDate设置为小于今天的日期,为什么仍会发生这种情况?
提前致谢。

最佳答案

据我所知,GORM不会通过遵循belongsTo关系来自动填充关系。

我总是做以下事情。

def u=new User(...)
u.addToTasks(
title:"testingwork",
startDate:(new Date()-4),
endDate:...
)
u.save()

请注意,我还没有创建任务对象。我已经将值映射直接传递给addToX ...这强调所添加的对象属于User,应该由GORM实例化并保存。

关于grails - Spock测试用例与Grails中的域类没有关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com