使用 Spock 进行 Grails 测试 - 选择哪个模拟框架? [英] Grails Testing with Spock - Which Mocking Framework Select?

查看:12
本文介绍了使用 Spock 进行 Grails 测试 - 选择哪个模拟框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有更一般的问题.使用 Spock 时,我应该使用哪个框架或实现在 Grails 2.x 中进行模拟?

I have more general question. Which framework or implementation I should use for mocking in Grails 2.x when using Spock?

我知道大量的模拟风格:利用 Groovy metaClass、Grails mockFor()、Groovy Mock()、Groovy 闭包风格等.它们每个都有自己的优点和缺点.但我不明白的是,某些嘲笑风格在我无法确定的某些场合下有效(即,mockFor() 适用于某些实现而不适用于其他实现).

I know tons of mocking style: leverage Groovy metaClass, Grails mockFor(), Groovy Mock(), Groovy closure style, etc. Each of them has its own advantages and disadvantages. But what I don't understand is that some mocking style works in certain occasions which I cannot determine (i.e. mockFor() works for certain implementation and not for the others).

目前我有两个类似的服务方法模拟实现.

Currently I have two similar implementation of service method mocking.

这个有效:

@TestFor(MyController)
@Mock([MyDevice])
class MyControllerSpec extends ControllerSpec {

void "test st."() {
      def myService = mockFor(MyService)
      myService.demand.myMethod() { def st ->
         return "test"
      }
      controller.myService = myService.createMock()
}
}

然而,这个实现不起作用:

However, this implementation doesn't work:

@TestFor(MyController)
@Mock([MyDevice])
class MyControllerSpec extends ControllerSpec {

void "test st."() {
      def yourService = mockFor(YourService)
      yourService.demand.yourMethod() { def st ->
         return "test"
      }
      controller.yourService = yourService.createMock()
}
}

服务实现和从控制器调用非常相似.那么在 Grails 中模拟的最佳实践是什么?或者是否有任何适用于 Grails 的良好模拟框架可以节省我弄清楚如何模拟的时间?

The service implementation and calling from controller is quite similar. So what is the best practice of mocking in Grails? Or is there any GOOD mocking framework for Grails which would save my time figuring out how to mock?

感谢您的建议!:-)

马特奥

推荐答案

当您使用 spock 框架进行测试时,请尝试利用框架本身提供的选项和样式.

When you are using spock framework for testing, then try to leverage the options and styles provided by the framework itself.

Spock 框架有助于实现 BDD [行为设计开发].我所说的行为是指您可以将业务接受场景与开发周期紧密结合.

Spock framework helps in achieving a BDD [Behavioral Design Development]. By behavior I meant, you can tightly couple the business acceptance scenario to the development cycle.

我尝试用 Spock 编写您的测试用例,因为它可以重写为:

I tried to get your test case written in Spock as it can be re-written as:

@TestFor(MyController)
class MyControllerSpec extends ControllerSpec {

    void "test service method in called once and only once"(){
        //Defines the behavior of setup
        setup: "Only one invocation of service method" 
            def myService = Mock(MyService){
                //Make sure serviceMethod is called only once from the controller
                //Beauty of Spock is that you can test the cardinality of
                //method invocations.  
                1 * serviceMethod() >> "Hello"
            }
            controller.myService = myService

            //The above process can be followed 
            //for stubbing/mocking 'YourService' and 
            //then injecting it to controller like
            //controller.yourService = yourService

        //Defines the behavior of action
        when: "controller action is called" 
            controller.myAction()

        //Defines the behavior of expected results
        then: "Expect the stubbed service method output" 
            controller.response.contentAsString == "Hello"
    }
}

//Controller
def myAction() {
    render myService.serviceMethod()
}

如果你看到上面的内容,你就定义了每一步的行为.从企业的角度来看,这些行为将由 BA 或利益相关者驱动.通过这种方式,您符合 ATDD/BDD(验收测试驱动开发/行为测试驱动开发)并最终为您的项目提供 TDD(测试驱动开发).

If you see above, you define the behavior in each step. From an enterprise perspective, those behavior will be driven by BA or the stakeholders. By this way, you comply to ATDD/BDD(Acceptance Test Driven Dev/Behavior Test Driven Dev) and eventually a TDD(Test Driven Dev) for your project.

有关如何有效使用 spock 框架的更多详细信息,请访问 spock 框架docs.

For more details on how to use spock framework effectively, visit spock framework docs.

这篇关于使用 Spock 进行 Grails 测试 - 选择哪个模拟框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆