Grails

A little help with controller mocking in unit tests

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Duplicate
  • Affects Version/s: 1.0.1
  • Fix Version/s: 1.1-beta1
  • Component/s: Testing
  • Labels:
    None
  • Environment:
    N/A

Description

Glen Smith posted a nice mini-tutorial on unit testing controllers here: http://blogs.bytecode.com.au/glen/2008/03/12/mockfor-march---unit-testing-grails-controllers.html. It seems like some of the boilerplate mocking could be taken care of by grails. It would seem to save some repeating one's self when coding unit tests. Graeme suggested the following in the comments:

I think we should have a new ControllerTestCase that has a method like

def withMockController(Class c, Closure c)

that does all of the setting up of params/flash/etc.

Then you could just do

withMockController(LoginController) {
def controller = new LoginController()
lc.login(goodUser)

assertEquals "glen", session.account.userId
assertEquals "/demo/", redirectParams.uri
assertNull flash.loginError

LoginCommand badUser = new LoginCommand(userId: 'glen', password: 'unlucky')
lc.login(badUser)

assertNull session.account
assertNotNull flash.loginError
}

A definite improvement.

  1. AccountController.groovy
    13/Mar/08 12:28 PM
    0.5 kB
    Kevin Burke
  2. AccountControllerTests.groovy
    13/Mar/08 12:28 PM
    2 kB
    Kevin Burke
  3. ControllerTestCase.groovy
    13/Mar/08 4:04 PM
    4 kB
    Kevin Burke
  4. ControllerTestCase.groovy
    13/Mar/08 12:28 PM
    1.0 kB
    Kevin Burke

Activity

Hide
Kevin Burke added a comment -

I don't want to be too hasty to post up an implementation, but here is my first stab. Attached is the ControllerTestCase, a possible test class that extends ControllerTestCase and the Controller that it is testing.

Show
Kevin Burke added a comment - I don't want to be too hasty to post up an implementation, but here is my first stab. Attached is the ControllerTestCase, a possible test class that extends ControllerTestCase and the Controller that it is testing.
Hide
Kevin Burke added a comment -

Too hasty indeed. ControllerTestCase will need to mock all of the dynamic controller methods provided in ControllersGrailsPlugin to be viable. My bad. I'll update this when I get the chance.

Show
Kevin Burke added a comment - Too hasty indeed. ControllerTestCase will need to mock all of the dynamic controller methods provided in ControllersGrailsPlugin to be viable. My bad. I'll update this when I get the chance.
Hide
Kevin Burke added a comment -

Ok, talking to myself here . Anyway this ControllerTestCase should work better. It handles chain, render, bindData and other calls.

Show
Kevin Burke added a comment - Ok, talking to myself here . Anyway this ControllerTestCase should work better. It handles chain, render, bindData and other calls.
Hide
Bob Herrmann added a comment -

Nothing wrong with talking to yourself. Thanks for your work, I'm trying to use it right now!

Show
Bob Herrmann added a comment - Nothing wrong with talking to yourself. Thanks for your work, I'm trying to use it right now!
Hide
James Cook added a comment -

Isn't this taken care of with the new plugin: http://grails.org/Testing+Plugin
It could probably be closed.

Show
James Cook added a comment - Isn't this taken care of with the new plugin: http://grails.org/Testing+Plugin It could probably be closed.
Hide
Graeme Rocher added a comment -

Indeed it is, closing

Show
Graeme Rocher added a comment - Indeed it is, closing

People

Vote (20)
Watch (8)

Dates

  • Created:
    Updated:
    Resolved: