2014-07-23 224 views
2

我无法弄清楚如何测试控制器的动作是否“链”。我想验证这一行动。单元测试Grails控制器链接

的Grails:2.4.2

控制器:

class MyController { 

def index() { 

} 

def doesChain() { 
    chain action: 'index', model: [name: "my name"] 
} 

}

测试:

@TestFor(MyController) 
class MyControllerSpec extends Specification { 

def setup() { 
} 

def cleanup() { 
} 

void "Action doing chain"() { 

    when: 
    controller.doesChain() 

    then: 
    controller.chainModel.name == "my name" 
    controller.actionName == "someAction" // fails as actionName == null 
} 

}

测试动作的名称不传递s actionName看起来是空的。

回答

3

你可以做这样的事情......

@TestFor(MyController) 
class MyControllerSpec extends Specification { 

    void "Action doing chain"() { 

     when: 'an action invokes the chain method' 
     controller.doesChain() 

     then: 'the model is as expected' 

     // either of these should work, you don't need both... 
     controller.chainModel.name == "my name" 
     flash.chainModel.name == "my name" 

     and: 'the redirect url is as expected' 
     response.redirectUrl == '/my/index' 

    } 
} 

我希望帮助。

+0

谢谢!其实已经尝试过,但只有'/'作为redirectUrl。如何,从你的例子中得到灵感,我创建了新的控制器并对它进行测试,并且它可以工作!为我的其他控制器返回'/'的原因是它是UrlMappings.groovy中默认映射的:“/”(controller:“my”)。这个配置被拾取结束'我/ /索引'刚刚映射到'/'。所以问题解决了。谢谢! – raaputin