0

我有一个这样的控制器:

 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) 

我不知道我哪里错了。

提前致谢。

4

1 回答 1

0

听起来与使用 Spock 投射空对象的问题中讨论的问题相同。解决方案是升级到 Grails 2.0.2。

于 2012-08-04T13:30:22.400 回答