Asserting view with Grails Spock Unit Test for Controllers

Gregg
  • 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?

Raipe

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

grails Unit test spock issue

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

How to test the catch block code in Spock unit test Grails

From Dev

How to write a spock unit test case for traits in grails 2.4?

From Dev

Grails: how to save value to transient field while unit test with spock?

From Dev

Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

From Dev

Grails: how to save value to transient field while unit test with spock?

From Dev

Using Spock data driven tests in Grails 3 unit test

From Dev

How do I unit test iOS view controllers?

From Dev

Spock Mock not working for unit test

From Dev

Asserting complex spock interaction

From Dev

Unit Test Directive Controllers in AngularJS

From Dev

Unit Test Directive Controllers in AngularJS

From Dev

How to test Grails service using Spock?

From Dev

Passing actual parameters in a spock unit test specification

From Dev

Getting "Too few invocations" on unit test with spock

From Dev

Grails 2.3.9 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

From Dev

Grails 2.4.0 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

From Dev

Grails 2.4.0 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

From Dev

Grails 2.3.9 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

From Dev

Grails Unit test and mocking addTo

From Dev

Stub method in grails unit test

From Dev

Grails Unit test and mocking addTo

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

From Dev

Grails : Spock : Unit testing GORM domain class hooks

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

One View and Multiple Domains/Controllers in Grails

From Java

Spock -Unit Test:How to write spock unit test for @around annotation which takes Mono

Related Related

  1. 1

    grails Unit test spock issue

  2. 2

    Grails Spock Mock Injected Service in Unit Test

  3. 3

    How to test the catch block code in Spock unit test Grails

  4. 4

    How to write a spock unit test case for traits in grails 2.4?

  5. 5

    Grails: how to save value to transient field while unit test with spock?

  6. 6

    Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

  7. 7

    Grails: how to save value to transient field while unit test with spock?

  8. 8

    Using Spock data driven tests in Grails 3 unit test

  9. 9

    How do I unit test iOS view controllers?

  10. 10

    Spock Mock not working for unit test

  11. 11

    Asserting complex spock interaction

  12. 12

    Unit Test Directive Controllers in AngularJS

  13. 13

    Unit Test Directive Controllers in AngularJS

  14. 14

    How to test Grails service using Spock?

  15. 15

    Passing actual parameters in a spock unit test specification

  16. 16

    Getting "Too few invocations" on unit test with spock

  17. 17

    Grails 2.3.9 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

  18. 18

    Grails 2.4.0 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

  19. 19

    Grails 2.4.0 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

  20. 20

    Grails 2.3.9 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

  21. 21

    Grails Unit test and mocking addTo

  22. 22

    Stub method in grails unit test

  23. 23

    Grails Unit test and mocking addTo

  24. 24

    How to mock render in (Spock, Grails) unit testing Taglib?

  25. 25

    Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

  26. 26

    Grails : Spock : Unit testing GORM domain class hooks

  27. 27

    How to mock render in (Spock, Grails) unit testing Taglib?

  28. 28

    One View and Multiple Domains/Controllers in Grails

  29. 29

    Spock -Unit Test:How to write spock unit test for @around annotation which takes Mono

HotTag

Archive