Grails 1.3.7中的单元测试控制器:redirectUrl为null或params不可用

时间:2012-05-29 18:54:43

标签: unit-testing grails grails-controller

我有以下代码:

void testConfirmBookNotFound() {

    def book = new Book(id: 1, title: 'test book')
    mockDomain(Book, [book])
    mockDomain(BookProject, [new BookProject(id: 1, book: book)])

    def controller = new CancelController()
    controller.params.id = 5140
    controller.confirm()

    assertEquals "/admin/cancel/index", controller.response.redirectedUrl
}

如果我的测试类延伸ControllerUnitTestCase,则controller.response.redirectedUrl始终为null。但是,如果我的测试类扩展GrailsUnitTestCase(我已经阅读过这是针对null url问题的修复),那么我得到No such property: params。我需要做些什么来使这个测试起作用?

1 个答案:

答案 0 :(得分:2)

尝试使用controller.redirectArgs.action,如...

assertThat(controller.redirectArgs.action, equalTo("index"))