Grails 2.0 Modules for unit testing: commissioning and depends on

Currently updating the grails 1.3.7 application to 2.1.0 and a set of filters that I would like to check. As a grails document for filter testing , it is suggested that now module testing for filters is supported (/ recommended - mentioned in the functional part, but does not find examples), I'm trying to convert some existing integration tests for a filter into unit tests.

Nevertheless, I do my best to โ€œbrush offโ€ the filter of this filter dependsOn /, at least correctly implement the layout for some service that is entered by the filters.

 package com.example import ... // excluded for brevity class MyFilters { GrailsApplication grailsApplication SomeService someService def dependsOn = [MyOtherFilters] def filters = { all(controller: 'controllerToExclude', invert: true) { before = { if (grailsApplication.config.someConfigProperty) { def someProperty = request.getAttribute('MY_ATTRIBUTE') if (someProperty = someService.someMethod()) { redirect(url: someService.getNewUrl(session)) return false } } return true } } } } 

And another filter:

 package com.example class MyOtherFilters { SomeOtherService someOtherService def filters = { all(controller: '*', action: '*') { before = { def foo if (params[MY_ATTRIBUTE]) { foo = params[MY_ATTRIBUTE] someOtherService.setMyAttribute(sitePreference, request) } if (!foo) { foo = someOtherService.getMyAttribute(request) } if (foo) { request.setAttribute('MY_ATTRIBUTE', foo) } return true } } } } 

This is a really skeletal simplified version of the two filters I work with (if anyone is interested, they read the preferences of mobile and desktop computers, and then filter based on this preference).

So, the test I'm writing looks something like this:

 package com.example import grails.test.mixin.TestFor // ... etc more imports here @TestFor(SomeController) @Mock(MyFilters) // TODO what goes here??? class MyFiltersTests { static final IPAD_USER_AGENT = 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X; en-us) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3' static final NON_MOBILE_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.12011-10-16 20:23:00' void testFilterRedirects() { grailsApplication.config.someConfigProperty = true // actual filter logic acts on this user-agent through some service calls, would like to mock it out though request.addHeader("user-agent", IPAD_USER_AGENT) def result withFilters(action: "index") { result = controller.index() } //assertSomething on result perhaps assertEquals "myExpectedRedirectUrl", response.redirectedUrl } } 

Since this code is worth it, it doesn't even execute MyFilters code. I tried adding dependent filters to the mocking ala:

 @Mock([MyFilters, MyOtherFilters]) 

But then I ran into problems with SomeOtherService methods that were not defined, and did not find a way to correctly trick these methods (how to set service layouts on a filter? On a controller or service that you can def myMock = mockFor(SomeOtherService) , and then run controller.someOtherService = myMock.createMock() or something like that, but I canโ€™t find a way to install the service for the filter using this withFilters block suggested for documentation.

Ideally, I would scoff at what needs to be done with someService and MyOtherFilters , and just wrote down my test for this filter, but I'm not sure what possible testing / testing filters are.

Any insight would be greatly appreciated, thanks if you made it this far!

+7
source share
1 answer

Have the same problem. An error appears in Grails Jira: http://jira.grails.org/browse/GRAILS-8976

I found a workaround at http://delvingintodev.carrclan.us/2012/12/testing-grails-filters-that-use-services.html 'Testing Grails Filters That Use the Services'

Basically you should use the service in the filter as follows

 package xxx.me class MyFilters { def filters = { all(controller:'*', action:'*') { before = { applicationContext.getBean(MyService).doSomethingClever() } } } } 

In this case, you can mock him in unit tests.

 package xxx.me @TestMixin(GrailsUnitTestMixin) @Mock(MyFilters) class MyFiltersTests { @Test public void testFilter(){ defineBeans { myService(StubbedMyService) } SimpleController controller = mockController(SimpleController); withFilters(controller:"simple" , action:"index"){ controller.index() } } } class StubbedMyService extends MyService { def doSomethingClever(){ } } 
+2
source

All Articles