gpt4 book ai didi

grails - @TestFor和扩展ControllerUnitTestCase之间的区别

转载 作者:行者123 更新时间:2023-12-02 14:33:29 24 4
gpt4 key购买 nike

我正在使用grails 2.2.4,它相对较新。我正在尝试对 Controller 进行单元测试,但我在书本和网络上看到的内容感到困惑。有时我看到人们写这样的 Controller 单元测试:

@TestFor(MyController)
@Mock([Domain1, Domain2])
class MyControllerUnitTests {
void testOne() ...
}

有时我看到这个:
class MyControllerUnitTests extends ControllerUnitTestCase {
public void setUp() ...
public void tearDown() ...
}

有什么区别,对于2.2.4中的单元测试,有什么区别?他们俩似乎都工作。

最佳答案

在Grails 2中引入了混合测试的用法,而您提到的另一种方法已在较早版本的Grails中使用。查看官方Grails文档中的以下说明:

Grails 1.3.x and below used the grails.test.GrailsUnitTestCase class hierarchy for testing in a JUnit 3 style. Grails 2.0.x and above deprecates these test harnesses in favour of mixins that can be applied to a range of different kinds of tests (JUnit 3, Junit 4, Spock etc.) without subclassing



http://grails.org/doc/latest/guide/single.html#testing

因此,您应该在Grails 2.2.4中使用mixins进行单元测试。总体上,更多有关Grails中可用的mixin和单元测试的信息: http://grails.org/doc/latest/guide/single.html#unitTesting

关于grails - @TestFor和扩展ControllerUnitTestCase之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162757/

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