Test before Interceptor in the integration test in rake

Is there any way to test this interceptor? This is ignored in my test.

code:

class BaseDomainController { def beforeInterceptor = { throw new RuntimeException() if(!isAdmin()){ redirect(controller: 'login', action: 'show') return } } } class BaseDomainControllerSpec extends IntegrationSpec{ BaseDomainController controller = new BaseDomainController() def 'some test'(){ given: controller.index() expect: thrown(RuntimeException) } } 
+4
source share
2 answers

According to this thread http://grails.1312388.n4.nabble.com/Controller-interceptors-and-unit-tests-td1326852.html Graham indicates that you need to call the interceptor separately. In our case, since we use an interceptor to verify the token, and it is the same for each action, we used:

 @Before void setUp() { super.setUp(); controller.params.token = "8bf062eb-ec4e-44ae-8872-23fad8eca2ce" if (!controller.beforeInterceptor()) { fail("beforeInterceptor failed"); } } 

I assume that if each unit test sets different arguments for the interceptor, you will have to call it separately each time. If you do not want this, I think you need to use something like Grail functional testing, which will go through the whole life cycle: http://grails.org/plugin/functional-test

+3
source

Grails documentation says:

Grails does not invoke hooks or servlet filters when invoking actions during integration testing. You should test interceptors and filters in isolation using functional testing, if necessary.

This also applies to block tests; certain interceptors do not affect the actions of your controller.

Given that you have:

  def afterInterceptor = [action: this.&interceptAfter, only: ['actionWithAfterInterceptor','someOther']] private interceptAfter(model) { model.lastName = "Threepwood" } 

To check the interceptor, you must:

Make sure interception is applied to the desired actions.

 void "After interceptor applied to correct actions"() { expect: 'Interceptor method is the correct one' controller.afterInterceptor.action.method == "interceptAfter" and: 'Interceptor is applied to correct action' that controller.afterInterceptor.only, contains('actionWithAfterInterceptor','someOther') } 

Make sure the interceptor method has the desired effect.

 void "Verify interceptor functionality"() { when: 'After interceptor is applied to the model' def model = [firstName: "Guybrush"] controller.afterInterceptor.action.doCall(model) then: 'Model is modified as expected' model.firstName == "Guybrush" model.lastName == "Threepwood" } 

Or, if you don't have interceptors, check to see if

 void "Verify there is no before interceptor"() { expect: 'There is no before interceptor' !controller.hasProperty('beforeInterceptor') } 

These examples were intended to be tested after interceptors, but the same applies to interceptors.

+1
source

Source: https://habr.com/ru/post/1436736/


All Articles