2014-05-05 116 views
2

我是grails新手。 我只是脚手架域类员工,其中低于grails单元测试spock问题

class Employee { 

    String firstName 
    String lastName 

    static constraints = { 
    } 
} 

我试图写在名单行动EmployeeController使用斯波克单元测试给出。下面

class EmployeeController { 

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"] 

    def index() { 
     redirect(action: "list", params: params) 
    } 

    def list(Integer max) { 
     params.max = Math.min(max ?: 10,100) 
     [employeeInstanceList: Employee.list(params), employeeInstanceTotal: Employee.count()] 
    } 
} 

控制器给出然后我写了下面

import grails.test.mixin.TestFor 
import spock.lang.Specification 

@TestFor(EmployeeController) 
class EmployeeControllerUnitSpec extends Specification { 

    def 'test index'() { 
     when: 
     controller.index() 

     then: 
     // httpcode ? 200 
     //println response (GrailsMockHttpServletResponse) 
     response.redirectedUrl == '/employee/list' 
    } 

    def 'test list empty'() { 
     when: 
     controller.list(10) 
     // Employee.list() 

     then: 
     model.employeeInstanceTotal == 0; 

    } 
} 

这里测试案例给出了指数正常工作的测试案例,但测试列表为空渲染控制台一些错误。 在控制台中的错误能解密是

| Running 2 spock tests... 2 of 2 
| Failure: test list empty(com.test.EmployeeControllerUnitSpec) 
| groovy.lang.MissingMethodException: No signature of method: com.test.Employee.list() is applicable for argument types:() values: [] 
Possible solutions: list(), list(java.util.Map), last(), last(java.lang.String), last(java.util.Map), first() 
    at com.test.EmployeeController.list(EmployeeController.groovy:15) 
    at com.test.EmployeeControllerUnitSpec.test list empty(EmployeeControllerUnitSpec.groovy:21) 
| Completed 2 spock tests, 1 failed in 3231ms 
| Tests FAILED - view reports in /mnt/hdd2/home/T-2060/workspace/testing/target/test-reports 

任何一个可以建议,如何才能解决这个问题

thankz提前

回答

2

单元测试环境将不会有域名可用,直到它被嘲笑。

使用@Mock(Employee)并在Employee中设置测试数据以测试list()动作。

+0

对不起,在哪里添加此语句@dmahapatro –

+1

您的意思是在哪里添加Mock?注释应该在课堂上进行。在grails中搜索@Mock。 – dmahapatro