Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: testing a redirect with an integration test

I'm using Grails 1.3.7. I'm trying to test a redirect in my integration test. Here is my controller and method in question ...

class HomeController {

def design = {
    ....
            if (params.page) {
                redirect(uri: "/#/design/${params.page}")
            }
            else {
                redirect(uri: "/#/design")
            }
            break;
    }
}

However in my integration test, the call to "controller.response.redirectedUrl" is failing (always returns null) even though I know the redirect call is being made (verified through logging). What is wrong with the integration test below?

class HomeControllerTests extends grails.test.ControllerUnitTestCase {
    ....

    void testHomePageDesign() { 
       def controller = new HomeController()

       // Call action without any parameters
       controller.design()

       assert controller.response.redirectedUrl != null

       assertTrue( responseStr != "" )
    }   

Thanks, - Dave

like image 535
Dave Avatar asked Nov 04 '22 15:11

Dave


1 Answers

Changing your HomeControllerTests to extend GrailsUnitTestCase should fix the problem.

class HomeControllerTests extends grails.test.GrailsUnitTestCase {
    ....
}

The various ways of generating a test class all seem to vary the class that is extended.

create-integration-test => GroovyTestCase
create-unit-test => GrailsUnitTestCase
create-controller => ControllerUnitTestCase

However, according to the Test section of the Grails User Guide, GrailsUnitTestCase is the core part of the testing frame and, at least in 1.3.7, that is the best class to base test classes on.

like image 60
John Wagenleitner Avatar answered Nov 09 '22 07:11

John Wagenleitner