2014-05-05 141 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