Wednesday, August 13, 2008

Grails Tests: Cannot send redirect - response is already committed

When testing controllers, if you make more than one call to a action/closure you may get the error: "Cannot send redirect - response is already committed"
But if you want to call one action to modify the database and then test the results in a subsequent call, if you put that call in a different test method, the prior test's transaction will probably be rolled back. So you want to make both action/closure calls in the same test method.

To get around this mess I simply MOPed it up (with help from Venkat Subramaniam's book "Programming Groovy"):

controller.metaClass.redirect = { Map args -> return args}

Note that the action of the redirect will not be invoked.
Also note that I am MOPing an instance, not the Class.

No comments: