Does anyone knows what happened to controller.redirectArgs.action in the latest version of Grails (1.3.3)? It used to work properly but now I get NPE when I use it.
class FooController {
def someRedirect = {
redirect(action:"bar")
}
}
class FooControllerTests exte开发者_JS百科nds grails.test.ControllerUnitTestCase {
void testSomeRedirect() {
controller.someRedirect()
assertEquals "bar", controller.redirectArgs.action
}
}
In this case controller.redirectArgs is already null...
I was having the same problem with the comparing the action to a string. the following worked for me, where
The controller looks something like:
class SomeObjectController {
def index = { redirect(action:list,params:params) }
def list = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
[
someObjectInstanceList: someObject.list( params ),
somObjectInstanceTotal: someObject.count()
]
}
}
and the test class would look something like:
class SomeObjectControllerTests extends ControllerUnitTestCase {
void testIndexRedirectToListAction(){
controller.index()
assertEquals controller.list, controller.redirectArgs.action
}
}
Funny, I followed the documentation here: http://www.grails.org/Testing+Controllers
I'm calling: assertEquals "nextAvailable", controller.redirectArgs.action
and I'm getting the following:
junit.framework.AssertionFailedError: junit.framework.AssertionFailedError: expected:<nextAvailable> but was:<com.***.***.XxxxXxxxController$_closure1@3da2cda9>
I seem to be getting back a closure and I'm trying to figure out how to get the action name.
I found the solution here: www.ibm.com/developerworks/java/library/j-grails10209/index.html?ca=drs-
If you do a quick ctrl/cmd + F, you'll find that this assert passes:
assertEquals controller.nextAvailable, controller.redirectArgs.action
This passes as well:
assertEquals controller.nextAvailable, controller.redirectArgs[action]
精彩评论