【问题标题】:Grails 2.2.2: Testing interceptors in ControllerTests with GMockGrails 2.2.2:使用 GMock 在 ControllerTests 中测试拦截器
【发布时间】:2013-06-25 02:45:45
【问题描述】:

当我们的代码库还在 Grails 2.1.4 中时,我们的拦截器的这个单元测试可以完全覆盖:

@Before
void setUp() {
   gmc = new GMockController()
   apiProducts = gmc.mock()

   api.products.returns(apiProducts).stub()

   controller.api = api
}

void testBeforeInterceptor() {
   // Arrange
   setUp()
   def ctrl = gmc.mock(controller)
   params.id = 1
   ctrl.actionName.returns('view')
   apiProducts.get(params.id)

   // Act
   gmc.play {
       controller.beforeInterceptor()
   }
}

切换到 Grails 2.2.2 后,这似乎不像以前在 2.1.4 中那样涵盖测试。错误“意外的方法调用”被调出,因为它不会像在 2.1.4 中那样通过拦截器。

拦截器的测试方式有变化吗?

我四处寻找,似乎没有人知道这个问题的答案。关于测试拦截器的文档很少。有什么想法吗?

【问题讨论】:

    标签: unit-testing grails groovy controllers gmock


    【解决方案1】:

    Grails 在调用时不调用拦截器或 servlet 过滤器 集成测试期间的操作。你应该测试拦截器和 单独过滤,必要时使用功能测试。

    (未经测试)我不确定这是否适用于 GMock。但integration tests doc 另有说明。

    【讨论】:

    • 我也对此进行了调查,但感谢您提出 :)
    猜你喜欢
    • 2019-06-29
    • 2016-03-20
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2020-01-03
    相关资源
    最近更新 更多