tags:

views:

95

answers:

1
+1  Q: 

TDD grails views

I want to write a gsp file that takes a domain object and generates KML. I want to verify that specific elements are present depending on the domain object contents. How would I TDD this gsp file? I was thinking about writing a test that called the render method with a domain object but not sure how to call the render method from outside of a controller. Is there another way to render a gsp page?

A: 

I found the answer after searching the "Grails Framework Reference Documentation" at http://grails.org/doc/latest/. Below is a code snippet that allowed for TDD of GSPs. This test is an integration test

class MyGspTest extends GroovyPagesTestCase {

    ...

    void testGsp() {
        //Open file containing GSP under test
        def myGspFile = new File("grails-app/views/myView/myGsp.gsp")
        assertNotNull(myGspFile)
        def template = myGspFile.text

        //Populate domain object with relevent test data
        MyDomainObject obj = new MyDomainObject()
        ...

        //Obtain result of GSP page
        def result = applyTemplate(template, [myDomainObject: obj])

        //Verify results contain expected output
        ...
    }

}
Nathan Reese