0
votes

I'm a pretty raw Grails newb but I know some Groovy and I have a Java background. This is an ancient test app I'm updating to Java 8 from Java 6.

Context: Grails 2.3.9 / Java 1.8.0_101 The test that is failing is:

@TestFor(PillsController)
@Mock(Pills)
class PillsControllerTests {
...
void testUpdate() {
    controller.update()

    assert flash.message != null
    assert response.redirectedUrl == '/somethingPills/list'

    response.reset()

    populateValidParams(params)
    def somethingPills = new SomethingPills(params)

    assert somethingPills.save() != null

    params.id = somethingPills.id

    controller.update()

    assert view == "/somethingPills/edit" //<--- FAILS HERE. VIEW IS NULL.
    assert model.somethingPillsInstance != null

    somethingPills.clearErrors()

    populateValidParams(params)
    controller.update()

    assert response.redirectedUrl == "/somethingPills/show/$somethingPills.id"
    assert flash.message != null

    response.reset()
    somethingPills.clearErrors()

    populateValidParams(params)
    params.id = somethingPills.id
    params.version = -1
    controller.update()

    assert view == "/somethingPills/edit"
    assert model.somethingPillsInstance != null
    assert model.somethingPillsInstance.errors.getFieldError('version')
    assert flash.message != null
}
}

I assume "view" is a reference to some codified variant of Model/View/Controller.

I found the update at the top confusing. Update what if nothing has been saved? I tried moving populate and SomethingPills declaration above update. The result was that view was still null. How can I predict what view will be?

Another thing I noticed. If I click on the update method, I see that there are two parameters. I'm assuming that Groovy allows you pass no parameters by default or I would be seeing an error about that. I don't know if this is how it is supposed to work but if I pull the ID & version from SomethingPills and pass them then I get /somethingPills/show/1" instead of null but still not "/somethingPills/edit".

[EDIT] I've found this: http://docs.grails.org/2.3.9/guide/scaffolding.html

1

1 Answers

0
votes

Go to your PillsController.groovy file and find the update() method.

See if you have a statement like

render view:'/somethingPills/edit', ....

In the unit test, view refers to the path of the gsp file that the controller renders.

I am pretty sure there would be some logic in the update() method, which you need to post if the unit test still fails. Form the unit test, it looks like as if some condition matches, then it renders the gsp template otherwise it redirects.