我有一个 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
}
但是当我运行我的测试并且我的测试失败时(没有出现错误消息,但它在then
我的 Spock 测试块中失败)并且我发现其中有错误。我的设置不会在 and 之间创建任何关系User
,Tasks
这使我的测试失败。
我要测试的控制器代码是:
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
我的测试比今天的日期少,也会发生这种情况?提前致谢。