使用Joda Time进行Grails控制器单元测试

时间:2013-07-20 12:34:41

标签: unit-testing grails controller jodatime

当我有一个带有Joda LocalDateTime字段的域对象时,生成所有创建的一些控制器测试失败。

$ grails create-app bugdemo
$ cd bugdemo
$ grails create-domain-class Item

编辑grails-app / domain / bugdemo / Item.groovy

package bugdemo
import org.joda.time.LocalDateTime

class Item {
    String name
    // LocalDateTime opening
    static constraints = {
        name blank:false
    }
}

将以下行添加到BuildConfig.groovy

compile ":joda-time:1.4"

继续执行命令行

$ grails compile
$ grails install-joda-time-templates
$ grails generate-all bugdemo.Item
$ grails test-app *ItemController

报告了几个错误。通过编辑ItemControllerTests

的TODO区域来修复这些问题
def populateValidParams(params) {
    assert params != null
    // TODO: Populate valid properties like...
    //params["name"] = 'someValidName'
    params["name"] = "Name"
}

   // test invalid parameters in update
    params.id = item.id
   //TODO: add invalid values to params object
    params.name = ""

现在所有的控制器测试都通过了。

现在取消注释Item.groovy中的LocalDateTime字段。那些控制器测试不再通过。我假设我需要添加更多的参数来填充开始对象。但是传递了什么参数?如果我运行应用程序,我可以查看表单并查看以下字段和值:

name: "Some Name"
opening: "struct" (hidden)
opening_day: "20"
opening_month: "7"
opening_year: "2013"
opening_hour: "22"
opening_minute: "20"

所以我修改了关闭populateValueParams中的代码,如下所示:

def populateValidParams(params) {
    assert params != null
    // TODO: Populate valid properties like...
    //params["name"] = 'someValidName'
    params["name"] = "Name"
    params["opening"] = "struct"
    params["opening_day"] = "20"
    params["opening_month"] = "07"
    params["opening_year"] = "2013"
    params["opening_hour"] = "22"
    params["opening_minute"] = "20"
}

当我运行测试时,错误仍然存​​在。似乎没有正确保存Item对象,可能是因为在测试环境中没有正确填充LocalDateTime字段(即使它在开发环境中)。

$ grails test-app *ItemController

| Running 8 unit tests... 2 of 8                                                            
| Failure:  testShow(bugdemo.ItemControllerTests)                                           
|  Assertion failed:                                                                        

assert item.save() != null                                                                  
       |    |      |                                                                        
       |    null   false                                                                    
       bugdemo.Item : (unsaved)                                                             

        at bugdemo.ItemControllerTests.testShow(ItemControllerTests.groovy:69)              
| Running 8 unit tests... 3 of 8                                                            
| Failure:  testSave(bugdemo.ItemControllerTests)                                           
|  Assertion failed:                                                                        

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

        at bugdemo.ItemControllerTests.testSave(ItemControllerTests.groovy:55)              
| Running 8 unit tests... 6 of 8                                                            
| Failure:  testEdit(bugdemo.ItemControllerTests)                                           
|  Assertion failed:                                                                        

assert item.save() != null                                                                  
       |    |      |                                                                        
       |    null   false                                                                    
       bugdemo.Item : (unsaved)                                                             

        at bugdemo.ItemControllerTests.testEdit(ItemControllerTests.groovy:87)              
| Running 8 unit tests... 7 of 8                                                            
| Failure:  testUpdate(bugdemo.ItemControllerTests)                                         
|  Assertion failed:                                                                        

assert item.save() != null                                                                  
       |    |      |                                                                        
       |    null   false                                                                    
       bugdemo.Item : (unsaved)                                                             

        at bugdemo.ItemControllerTests.testUpdate(ItemControllerTests.groovy:107)           
| Running 8 unit tests... 8 of 8                                                            
| Failure:  testDelete(bugdemo.ItemControllerTests)                                         
|  Assertion failed:                                                                        

assert item.save() != null                                                                  
       |    |      |                                                                        
       |    null   false                                                                    
       bugdemo.Item : (unsaved)                                                             
| Packaging Grails application.....                                                         

是否需要嘲笑Joda类的某些方面?当控制器说

    def itemInstance = new Item(params)

为什么它在dev中工作,而不是测试? 如何在开发中工作?

我使用的是Grails 2.2.3,Java 1.7.0_25,Windows 7。

2 个答案:

答案 0 :(得分:3)

单元测试不会自动设置自定义属性编辑器(负责转换域类实例中的提交信息)。

查看来源,似乎JodaTimePropertyEditorRegistrar对此负责。因此,在您的测试中,您可以使用defineBeans添加此Spring Bean:

import grails.plugin.jodatime.binding.JodaTimePropertyEditorRegistrar

class MyTest {
  @Before
  public void setup() {
    defineBeans {
      jodaTimePropertyEditorRegistrar(JodaTimePropertyEditorRegistrar)
    }
  }
}

答案 1 :(得分:0)

您可以使用对象在populateValidParams()方法中设置DateTime()或其他joda-time值...

def populateValidParams(params) {
    assert params != null
    params["name"] = "Name"
    params["opening"] = new DateTime()
}