views:

254

answers:

2

Hey i am trying to develop a sample app in groovy on grails...i have an action called login..which doesn't do anything except to render a page called login...I think there is no need to explicitly render any view called login inside the action, as my view name matches the action name.

def login = {

}

As i follow TDD..I want to assert that that response was successful..How do i do that in groovy unit testing?

+2  A: 

You can't unit test the HTTP response, as the controller is just a plain old groovy class when called from a unit test. You'll need to use an integration test (using a MockHttpServletRequest/Response) or use one of the functional testing plugins: gfunc, webtest, selenium etc

There should be plenty of examples of both integration and functional tests to be found via Google.

cheers

Lee

leebutts
The Grails User Guide is a good place to start. An example of integration testing controllers: http://www.grails.org/doc/latest/guide/9.%20Testing.html#9.2%20Integration%20Testing
miek
A: 

when i am able to test the response content like this "controller.response.contentAsString " in a unit test,why cant i test the status of the view rendered by the controller?..I just want to assert that my response was a success and not a 404..I feel it should be possible

prabha
How about response.status? I don't how much sense this makes in a mocked response, however. See the MockHttpServletResponse API docs: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mock/web/MockHttpServletResponse.html
miek
controller.repsonse can only be used when integration testing but not unit testing. When unit testing, you can only access/use the fields and methods you can see in the classes, non of the runtime magic is there (like the request and response properties).You can mock the runtime methods using the mockController method and ControllerUnitTestCase
leebutts