Dave
Dave

Reputation: 8897

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

Upvotes: 5

Views: 1828

Answers (1)

John Wagenleitner
John Wagenleitner

Reputation: 11035

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.

Upvotes: 2

Related Questions