Consider the following controller:
package test
class FirstController
{
def index =
{
chain(controller:"second",
action: "index")
}
}
... with the following associated unit test case:
package test
import grails.test.*
class FirstControllerTests extends ControllerUnitTestCase
{
protected void setUp(开发者_C百科)
{
super.setUp()
}
protected void tearDown()
{
super.tearDown()
}
// Passes
void testChainArgs()
{
controller.index()
assertEquals('index', controller.chainArgs.action)
assertEquals('second', controller.chainArgs.controller)
}
// Fails
void testOverrideInstanceChain()
{
def passTest = false
controller.metaClass.chain =
{
Map args ->
passTest = true
}
assertTrue(passTest)
}
// Fails
void testOverrideClassChain()
{
def passTest = false
FirstController.metaClass.chain =
{
Map args ->
passTest = true
}
assertTrue(passTest)
}
}
Why are the last two tests failing?
I am sorry, but you miss the invocation to the controller.index().
After I test out your code with
void testOverrideInstanceChain()
{
def passTest = false
controller.metaClass.chain =
{
Map args ->
passTest = true
}
controller.index()
assertTrue(passTest)
}
and
void testOverrideClassChain()
{
def passTest = false
FirstController.metaClass.chain =
{
Map args ->
passTest = true
}
controller.index()
assertTrue(passTest)
}
All tests pass.
精彩评论