Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting view with Grails Spock Unit Test for Controllers

  • Grails 2.2.4
  • Spock 0.7

I'm trying to test that the correct view is rendered from a grails controller. My create method looks like this:

def create() {
    def documentCategories = DocumentCategory.list()
    def documentTypes = DocumentType.list()
    def documentComponents = DocumentComponent.list()
    [documentCategories: documentCategories,
        documentTypes: documentTypes,
        documentComponents:documentComponents]
}

And my test:

def "test create action"() {
    given:
    def model = controller.create()

    expect:
    response.status == 200
    model.documentCategories.size() == 0
    model.view == '/document/create'
}

I've tried various versions of model.view including:

view == '/document/create'
response.forwardedUrl == '/document/create'

all of which fail because model.view, view, and response.forwardedUrl are all null. Suggestions?

like image 752
Gregg Avatar asked Jun 07 '26 13:06

Gregg


1 Answers

As you are not defining the view explicitly in the controller method then the Grails conventions will take place. Accordingly to documentation a view matching the name of the controller and method will be chosen --> view: "controllerName/methodName"

Regarding your problem, you should not be testing that the Grails framework is working. You should be testing that your controller behaves as you want.

In this case you want to test that your controller does not specify the view as that is the expected behavior of your controller.

Test for this would be:

   then:
    controller.modelAndView == null
    response.redirectedUrl == null

As the modelAndView will be created if you would call the 'render(view: xxx)' in your controller.

Calling redirect() or chain() in your controller results to response.redirectedUrl to be populated in your unit test

like image 195
Raipe Avatar answered Jun 10 '26 11:06

Raipe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!