我对grails不熟悉。我只是搭建了一个域类雇员,如下所示
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;
}
}这里,索引的测试用例工作正常,但是在控制台中测试list空呈现某些错误。控制台中的错误更正是
| 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有谁能建议,如何解决这个问题?
预先感谢
发布于 2014-05-05 12:04:16
在对域进行模拟之前,单元测试环境将无法使用域。
使用@Mock(Employee)并在Employee中设置测试数据来测试list()操作。
https://stackoverflow.com/questions/23466262
复制相似问题