2

ドメイン オブジェクトに Joda LocalDateTime フィールドがあると、generate-all create が作成するコントローラー テストの一部が失敗します。

$ 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"
}

テストを実行しても、エラーはまだ残っています。おそらく、LocalDateTime フィールドがテスト環境で正しく入力されていないため (開発環境であっても)、Item オブジェクトが正しく保存されていないようです。

$ 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)

なぜ開発では機能するのにテストでは機能しないのですか? 開発ではどのように機能しますか?

Grails 2.2.3、Java 1.7.0_25、Windows 7 を使用しています。

4

2 に答える 2

0

オブジェクトを使用して、populateValidParams() メソッドで DateTime() またはその他の joda-time 値を設定できます...

def populateValidParams(params) {
    assert params != null
    params["name"] = "Name"
    params["opening"] = new DateTime()
}
于 2014-08-16T12:55:48.773 に答える