views:

145

answers:

2

How do I test this:

render view: "create", model: [user: user]

I know how to test redirectArgs and stuff, but I can't find an example to test something like this. It seems like articles and stuff go out of their way to not test this....

+1  A: 

renderArgs is what you want. (From ControllerUnitTestCase)

E.g to test that the correct view will be rendered

assertEquals 'create', renderArgs.view

Check the model

assertEquals user, renderArgs.model.user

Make sure that your test extends ControllerUnitTestCase

Etc etc

tinny
Thanks. I hate the fact that groovy is dynamic, so code completion wouldn't just tell me this. And you'd think between ibm developer works and grails testing section, it would have mentioned this. I mean, it shows EVERY OTHER EXAMPLE except for this one... which is actually the most common! Makes me angry :/ Thank you very much tinny.
egervari
Dynamic typing has its trade offs, but makes so much of the Grails framework voodoo possible. "The Definitive Guide to Grails" has a good section on controller testing p91. Also renderArgs is mentioned in the API docs http://grails.org/doc/1.3.x/api/grails/test/ControllerUnitTestCase.html
tinny
A: 

Are you sure this works? I'm trying the incredibly trivial test below which does not work:

NotebookController:

def index = {
    log.info("redirecting to list")
    redirect(action: "list", params: params)
}

NotebookControllerTest

package net.codezilla.knodebook import grails.test.*

class NotebookControllerTests extends ControllerUnitTestCase { protected void setUp() { super.setUp() }

protected void tearDown() {
    super.tearDown()
}

 void testIndex() {
  controller.index()
  assertEquals "/notebook/list", renderArgs.view
}

}

Running this test gives the following result:

testIndex Failure junit.framework.ComparisonFailure: null expected: but was:

junit.framework.AssertionFailedError: junit.framework.ComparisonFailure: null expected: but was: at net.codezilla.knodebook.NotebookControllerTests.testIndex(NotebookControllerTests.groovy:24)

Patrick Haggood