我有一个像这样的控制器:
def unCompletedTasks() {
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('completed',false)
lt('endDate',new Date().clearTime())
}
}
[tasksInstanceList : search, tasksInstanceTotal: search.getTotalCount() ]
}
我想测试一下。我在Spock中写了一个测试规范:
def 'user should be displayed unCompletedTasks' () {
setup: "set the required objects"
def tasksController = new TasksController()
tasksController.springSecurityService = [principal: [id:tasksInstance.id]]
tasksController.params.managersProject = "testing"
//other codings goes here
when:
def model = tasksController.unCompletedTasks()
then:
model.tasksInstanceTotal == 1
where:
//required fields
}
当我跑步时,我收到这样的错误:
user should be displayed unCompletedTasks(mnm.schedule.TasksSpec)
| org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'org.codehaus.groovy.runtime.NullObject' to class 'java.lang.Integer'
at mnm.schedule.TasksController.unCompletedTasks(TasksController.groovy:39)
at mnm.schedule.TasksSpec.user should be displayed unCompletedTasks(TasksSpec.groovy:59)
我不知道哪里出错了。
提前致谢。