grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 951 forks source link

GRAILS-8248: Unit test withFilters not working for grails 2.0.0.RC1 #766

Closed graemerocher closed 9 years ago

graemerocher commented 12 years ago

Original Reporter: tianjianfeng Environment: Mac. Grails 2.0.0.RC1 Version: 2.0-RC1 Migrated From: http://jira.grails.org/browse/GRAILS-8248

Following the instructions for unit test for filters on http://grails.org/doc/2.0.0.RC1/guide/testing.html#unitTestingFilters

However, the withFilters closure is never called when running the test. Below is the code:

Filter class:

{code} class AuthenticationFilters { def filters = { create(controller:'openAm', action:'create') { before = { if (params.username == '') { render (status: HttpServletResponse.SC_BAD_REQUEST) return false } } } {code}

Controller unit test

{code} @TestMixin(GrailsUnitTestMixin) @TestFor(OpenAmController) @Mock (AuthenticationFilters) class OpenAmControllerTests { @Test void testCreateActionIsFiltered() { withFilters(action: "create") { println 'hello' controller.params.username = '' controller.create()

}
assert  400 == response.status 

} } {code}

graemerocher commented 12 years ago

graemerocher said: I attached an application demonstrating the filter testing is working in 2.0 RC1