简体   繁体   English

Grails 2.2.2:使用GMock在ControllerTests中测试拦截器

[英]Grails 2.2.2: Testing interceptors in ControllerTests with GMock

When our codebase was still in Grails 2.1.4, this unit test for our interceptors worked with full coverage: 当我们的代码库仍在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()
   }
}

After switching to Grails 2.2.2, it seems like this doesn't cover the test like it used to in 2.1.4. 切换到Grails 2.2.2之后,似乎并没有像2.1.4中那样覆盖测试。 The error "Unexpected method call" is being called out since it won't go through the interceptors like it used to in 2.1.4. 错误“意外的方法调用”被调出,因为它不会像在2.1.4中那样通过拦截器。

Was there a change in how interceptors are tested? 拦截器的测试方式是否有变化?

I've searched around and it seems like no one has the answer for this. 我到处搜寻,似乎没有人对此有答案。 The documentation on testing interceptors are sparse. 有关测试拦截器的文档很少。 Any ideas? 有任何想法吗?

Grails does not invoke interceptors or servlet filters when calling actions during integration testing. 在集成测试期间调用动作时,Grails不会调用拦截器或servlet过滤器。 You should test interceptors and filters in isolation, using functional testing if necessary. 您应该隔离测试拦截器和过滤器,必要时使用功能测试。

(Untested) I am not sure whether this is applicable here for GMock. (未经测试)我不确定这是否适用于GMock。 But integration tests doc mentions otherwise. 但是集成测试文档中另有提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM