unit-testing - 在 Controller 测试期间无法保存

标签 unit-testing grails testing grails-2.0 spock

我已经为此苦苦挣扎了一段时间,这似乎是一个普遍的问题,没有适合我的解决方案。

我想做的是测试调用 save() 的 Controller ,在 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() 时,测试失败并出现空错误。我花了太多时间试图解决这个问题,这一切都是徒劳的

这是输出截图

Error output

最佳答案

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

/*
 * in controller spec
 */
def setup() {
  def serviceMock = new MockFor(EmployeeManagerService)
  serviceMock.ignore('save') { ec, ac ->
    Employee.buildWithoutSave().save(validate: false)
  }
  serviceMock.use {
    controller.employeeManagerService = new EmployeeManagerService()
  }
}

def 'testFoo'() {
  when:
  // ...
  controller.employeeManagerService.save()
  // ...
  then:
  // ...
}

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

关于unit-testing - 在 Controller 测试期间无法保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351307/

相关文章:

Android Studio 单元测试 : read data (input) file

php - 使用 phpunit 测试遗留代码

spring - Grails Spring Security 插件 - 保护除根路径之外的所有内容

database-design - Grails域可以继承吗?

javascript - 如何使用 Cypress 为 Vue Tailwind CSS 应用程序编写 2e2 测试?

php - 对类使用 @covers

php - Magento/PHPUnit - 强制异常

grails - grails,当字段包含null时从db加载域会导致setter失败

android - Android 2.1 测试推荐设备

javascript - 如何使用 postman 查找 API 响应中返回的节点数?