jeudi 4 juin 2015

Grails 2.4.4 generated controller test fails

I have test for the save action of a controller. It just executes the action with correct params, but the problem is on the redirectedUrl line: it is null.

Using the app, after saving the domain instance, I get the redirection to the show action and the show view is rendered correctly.

Any clues of what's the problem here?

The controller:

@Transactional(readOnly = true)
class FolderController {

    ...
    @Transactional
    def save(Folder folderInstance) {

        if (folderInstance == null) {
            notFound()
            return
        }

        if (folderInstance.ehrId)
        {
           def ehr = ehr.Ehr.get(folderInstance.ehrId)
           ehr.directory = folderInstance
           ehr.save() 
        }

        if (folderInstance.hasErrors()) {
            respond folderInstance.errors, view:'create'
            return
        }

        folderInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id])
                redirect folderInstance
            }
            '*' { respond folderInstance, [status: CREATED] }
        }
    }
    ...
}

The test:

@TestFor(FolderController)
@Mock(Folder)
class FolderControllerSpec extends Specification {

        ...
    void "Test the save action correctly persists an instance"() {

        when:"The save action is executed with a valid instance"
            response.reset()
            populateValidParams(params)
            def folder = new Folder(params)

            controller.save(folder)
            println folder.errors // no errors

        then:"A redirect is issued to the show action"
            response.redirectedUrl == '/folder/show/1'
            controller.flash.message != null
            Folder.count() == 1
    }
    ...
}

The output:

junit.framework.AssertionFailedError: Condition not satisfied:

response.redirectedUrl == '/folder/show/1'
|        |             |
|        null          false
org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@112b2f1

    at directory.FolderControllerSpec.Test the save action correctly persists an instance(FolderControllerSpec.groovy:61)

Aucun commentaire:

Enregistrer un commentaire