在测试控制器期间无法保存

时间:2015-05-20 13:35:38

标签: unit-testing grails testing grails-2.0 spock

我一直在努力解决这个问题,这似乎是一个常见的问题,没有适合我的解决方案。

我要做的是测试调用save()的控制器,并在save()内部调用服务以使用给定的命令对象保存员工。

这就是代码的样子:

 def save(EmployeeInputCommand employeeCommand, AddressCommand addressCommand) { 
    if (addressCommand.address.validate()) {
        println "Validated address input"
        employeeManagerService.save(employeeCommand, addressCommand)
        println "Employee should be saved"
        println "------------------------->"
        println "${employeeGetterService.each {errors}}"
        flash.message = "Employee has been successfully created!"
        redirect(action: "index")
    } else {
        println "Addrescommand didnt validate: ${addressCommand.errors}"
        flash.message = "Please input valid data!!!"
        redirect(action: "create", model: [employee: employeeCommand, address: addressCommand])
    }
}

该服务包含:

def save(EmployeeInputCommand employeeCommand, AddressCommand addressCommand) {
    def employee = new Employee(employeeCommand.properties)
    employee.address = addressCommand.address
    for (id in employeeCommand.s) {
        employee.addToSkills(Skill.get(id))
    }
    if (employee.validate()) {
        employee.address.save()
        employee.save()
    }
    else {
       return false
    }
}

当我尝试在我的应用程序中实际保存员工时,我知道这有效,但在单元测试过程中没有任何反应。

我的测试:

     def "Create an Employee with good params"() {
    given: "A valid employee command object"
    def employeeCommand = new EmployeeInputCommand(
            name: "John",
            surname: "Smith",
            birthdate: Date.parse("yyyy-MM-dd", "1992-06-08"),
            salary: 21000,
            s: [1, 3, 5]
    )

    and: "Also a valid address command object"
    def addressCommand = new AddressCommand(
            street: "Green Alley",
            houseid: "42",
            city: "Odense",
            county: "Fyn",
            postCode: "5000 C"
    )

    expect:
    addressCommand.validate()
    !Employee.findByNameAndSurname("John", "Smith")

    when: "Saving employee"
    request.method = "POST"
    controller.save(employeeCommand, addressCommand)
    Employee employee = Employee.findByNameAndSurname("John", "Smith")

    println Employee.list()

    then: "The employee is created, and browser redirected"
    Employee.count == 4
    employee
    employee.skills.size() == 3
    employee.address.postCode == "5000 C"
}

调用controller.save()时,测试失败,返回null错误。我花了太多时间试图解决这个问题,这一切都是徒劳的

这是输出屏幕截图

Error output

3 个答案:

答案 0 :(得分:1)

我不相信测试控制器应涵盖服务逻辑。如果我要为控制器编写单元测试,我会模拟服务,并在自己的单元/集成测试中单独测试服务。 例如:

    method.insertAfter( "someClass.someMethod( $_ );");

请注意,代码使用了优秀的Build Test Data plugin

答案 1 :(得分:0)

我认为问题在于单元测试中的服务。在单元测试中,您需要defineBeans闭包才能使用spring beans -

void "test the filter"() {
    setup:
    defineBeans {
        employeeManagerService(EmployeeManagerService) { bean ->
            bean.autowire = true
        }
    }

    when:
    ...

    then:
    ...
}

参考#Inject Services in Grails Unit Test

答案 2 :(得分:0)

您需要将测试作为集成测试而不是单元测试来执行。当您测试与数据库相关的逻辑(CRUD操作)时,需要进行集成测试,而不仅仅是模拟CRUD操作,这是单元测试的作用。