如何在grails测试中填充有效参数

时间:2012-02-12 18:15:05

标签: unit-testing grails groovy

在groovy和grails中有点新。我试图运行grails install-templates并看到生成的test.groovy看起来像这样......

`<%=packageName ? "package ${packageName}\n\n" : ''%>

import org.junit.*
import grails.test.mixin.*

@TestFor(${className}Controller)
@Mock(${className})
class ${className}ControllerTests {


    def populateValidParams(params) {
      assert params != null
      // TODO: Populate valid properties like...
      //params["name"] = 'someValidName'
    }

    void testIndex() {
        controller.index()
        assert "/$propertyName/list" == response.redirectedUrl
    }

    void testList() {

        def model = controller.list()

        assert model.${propertyName}InstanceList.size() == 0
        assert model.${propertyName}InstanceTotal == 0
    }

    void testCreate() {
       def model = controller.create()

       assert model.${propertyName}Instance != null
    }

    void testSave() {
        controller.save()

        assert model.${propertyName}Instance != null
        assert view == '/${propertyName}/create'

        response.reset()

        populateValidParams(params)
        controller.save()

        assert response.redirectedUrl == '/${propertyName}/show/1'
        assert controller.flash.message != null
        assert ${className}.count() == 1
    }

    void testShow() {
        controller.show()

        assert flash.message != null
        assert response.redirectedUrl == '/${propertyName}/list'


        populateValidParams(params)
        def ${propertyName} = new ${className}(params)

        assert ${propertyName}.save() != null

        params.id = ${propertyName}.id

        def model = controller.show()

        assert model.${propertyName}Instance == ${propertyName}
    }

    void testEdit() {
        controller.edit()

        assert flash.message != null
        assert response.redirectedUrl == '/${propertyName}/list'


        populateValidParams(params)
        def ${propertyName} = new ${className}(params)

        assert ${propertyName}.save() != null

        params.id = ${propertyName}.id

        def model = controller.edit()

        assert model.${propertyName}Instance == ${propertyName}
    }

    void testUpdate() {
        controller.update()

        assert flash.message != null
        assert response.redirectedUrl == '/${propertyName}/list'

        response.reset()


        populateValidParams(params)
        def ${propertyName} = new ${className}(params)

        assert ${propertyName}.save() != null

        // test invalid parameters in update
        params.id = ${propertyName}.id
        //TODO: add invalid values to params object

        controller.update()

        assert view == "/${propertyName}/edit"
        assert model.${propertyName}Instance != null

        ${propertyName}.clearErrors()

        populateValidParams(params)
        controller.update()

        assert response.redirectedUrl == "/${propertyName}/show/\$${propertyName}.id"
        assert flash.message != null

        //test outdated version number
        response.reset()
        ${propertyName}.clearErrors()

        populateValidParams(params)
        params.id = ${propertyName}.id
        params.version = -1
        controller.update()

        assert view == "/${propertyName}/edit"
        assert model.${propertyName}Instance != null
        assert model.${propertyName}Instance.errors.getFieldError('version')
        assert flash.message != null
    }

    void testDelete() {
        controller.delete()
        assert flash.message != null
        assert response.redirectedUrl == '/${propertyName}/list'

        response.reset()

        populateValidParams(params)
        def ${propertyName} = new ${className}(params)

        assert ${propertyName}.save() != null
        assert ${className}.count() == 1

        params.id = ${propertyName}.id

        controller.delete()

        assert ${className}.count() == 0
        assert ${className}.get(${propertyName}.id) == null
        assert response.redirectedUrl == '/${propertyName}/list'
    }
}`

因此我将其复制到相应的控制器测试并更改了所有$ {propertyName}和$ {className}。传递了testIndex()testList()和testCreate(),但testSave(),testUpdate(),testDelete()和testEdit()失败。我想我需要填写这一部分:

def populateValidParams(params) {
      assert params != null
      // TODO: Populate valid properties like...
      //params["name"] = 'someValidName'
    }

所以我做了,方法看起来像这样:

def populateValidParams(params) {
  assert params != null
  // TODO: Populate valid properties like...
  //params["name"] = 'someValidName'

  params["company"] = 'someCompany'
  params["position"] = 'somePosition'
  params["responsibilities"] = 'someResponsibility'
  params["startDate"] = new Date ('2007/01/01')
  params["endDate"] = new Date ('2010/01/01')

}

但测试仍然失败..我做错了吗?我认为我在使用params做错了..我对那种常规类型有点不熟悉.. 这是保存的堆栈跟踪

    junit.framework.AssertionFailedError: Assertion failed: 

assert response.redirectedUrl == '/employmentHistory/show/1'
       |        |             |
       |        null          false
       org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@737611

    at com.someapp.controller.EmploymentHistoryControllerTests.testSave(EmploymentHistoryControllerTests.groovy:63)

表示:

    junit.framework.AssertionFailedError: Assertion failed: 

assert employmentHistory.save() != null
       |                 |      |
       |                 null   false
       com.someapp.domain.EmploymentHistory : null

    at com.someapp.controller.EmploymentHistoryControllerTests.testShow(EmploymentHistoryControllerTests.groovy:78)

用于编辑:

    junit.framework.AssertionFailedError: Assertion failed: 

assert employmentHistory.save() != null
       |                 |      |
       |                 null   false
       com.someapp.domain.EmploymentHistory : null

    at com.someapp.controller.EmploymentHistoryControllerTests.testEdit(EmploymentHistoryControllerTests.groovy:97)

1 个答案:

答案 0 :(得分:1)

assert employmentHistory.save() != null失败表示您的EmploymentHistory域对象未经过验证。确保您的参数符合您为EmploymentHistory声明的所有约束。