gpt4 book ai didi

grails - Grails 中的集成测试 : what is the correct way?

转载 作者:行者123 更新时间:2023-12-03 01:58:35 27 4
gpt4 key购买 nike

我对 Grails 完全陌生,自从我大约 4 个月前开始目前的工​​作以来,它一直在测试功能。几周前对我进行测试培训的人离开了我们的团队,现在我独自进行测试。我逐渐发现,我接受的有关如何进行 Grails 集成测试的培训方式与我在论坛和支持小组中看到的人们的方式几乎完全不同。我真的可以使用一些关于哪种方式是正确/最好的指导。顺便说一句,我目前正在使用 Grails 2.4.0。

这是一个集成测试的示例模型,采用了我接受过的培训风格。这是我应该这样做的正确方式,甚至是最好的方式吗?

@Test
void "test a method in a controller"() {

def fc = new FooController() // 1. Create controller

fc.springSecurityService = [principal: [username: 'somebody']] // 2. Setup Inputs
fc.params.id = '1122'

fc.create() // 3. Call the method being tested

assertEquals "User Not Found", fc.flash.errorMessage // 4. Make assertions on what was supposed to happen
assertEquals "/", fc.response.redirectUrl

}

最佳答案

由于使用了 Grails 2.4.0,您可以利用 spock framework 的优势默认情况下。

Here 是示例单元测试用例,您可以对其进行建模以编写集成规范。

注意:

  • 集成规范转到测试/集成
  • 应该继承IntegrationSpec .
  • 不需要模拟。与单位规范相比,不使用@TestFor。
  • DI 可以得到充分利用。类级别的 def myService 将在中注入(inject)服务规范。
  • 域实体不需要模拟。

以上规范应如下所示:

import grails.test.spock.IntegrationSpec

class FooControllerSpec extends IntegrationSpec {

void "test a method in a controller"() {
given: 'Foo Controller'
def fc = new FooController()

and: 'with authorized user'
fc.springSecurityService = [principal: [username: 'somebody']]

and: 'with request parameter set'
fc.params.id = '1122'

when: 'create is called'
fc.create()

then: 'check redirect url and error message'
fc.flash.errorMessage == "User Not Found"
fc.response.redirectUrl == "/"
}
}

关于grails - Grails 中的集成测试 : what is the correct way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24248737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com