【问题标题】:Grails: Controller unit test, what should the action's responsibility be?Grails:控制器单元测试,动作的职责应该是什么?
【发布时间】:2021-06-09 20:19:15
【问题描述】:

在示例https://guides.grails.org/grails-controller-testing/guide/index.html 中,成功案例需要有效负载,但这不应该是要求,测试应该只是验证操作实际上应该做什么?

例子:

class OneController {

    def list(OneCommand command) {

        if (command.hasErrors()) {

            render command.errors as JSON
            return
        }

        respond oneService.save(command.param1) as JSON
    }
}

如果 command 有效,则必须调用 Stub(oneService.save()) 传递 command.param1 作为参数,返回必须是该服务的 JSON 返回。

如果命令无效,它应该以 JSON 格式返回 command.errors。

在另一个单元测试中,测试 OneCommand。

【问题讨论】:

    标签: unit-testing grails controller


    【解决方案1】:

    在示例中 https://guides.grails.org/grails-controller-testing/guide/index.html, 成功案例需要有效的有效负载,但这不应该 是一个要求,测试应该只是验证什么动作 究竟应该怎么做?

    提供有效的负载并不是唯一的测试方法,但它是一种常见的方法。

    正在测试的代码有 2 条路径。如果command.hasErrors() 评估为true,则触发一个路径,如果command.hasErrors() 未评估为true,则触发另一路径。测试应该涵盖这些路径中的每一个。提供具有有效负载的测试和具有无效负载的测试是实现此目的的简单方法。还有其他方法,例如可以在控制器上调用list 方法并将模拟的OneCommand 作为参数传递。

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多