2013-09-01 81 views
0

我有一個單元測試,我試圖讓驗證成功。validate()模擬約束在Grails中測試

基本上,當我打電話給badMockedSecureFile.validate()時,它沒有達到我期望的水平,而且這兩個字段encryptedFileNameencryptedFileData的驗證失敗。

當我在調試器中斷時,我只是在隨後的斷言中得到badMockedSecureFile.errorsnull值。這裏是我的兩個文件:

任何輸入將不勝感激。我找不到一個確切的類似問題。我正在使用grails 2.2.4Oracle JDK 1.7.0_25如果有任何問題。

編輯:我只是想指出,我刪除了mockForConstraintTests調用,它似乎現在工作。我感覺到這種感覺,我沒有在任何地方使用RTFM,這種行爲在單元測試中發生了變化,或者是其他事情正在發生?

SecureFile.groovy

class SecureFile implements Serializable { 

    /** 
    * An unencrypted version of the file name. This file name is unencrypted 
    * when the appropriate password and key combo is used and it is never 
    * persisted to the database for security (see transients below). 
    */ 
    String fileName 

    /** 
    * Unencrypted version of the file data. Never persisted to the 
    * database for security (see transients below). 
    */ 
    byte[] fileData 

    String encryptedFileName 
    byte[] encryptedFileData 
    Date dateAdded 
    Date dateUpdated 
    Date dateDeleted 

    static constraints = { 
     encryptedFileName(nullable: false, blank: false) 
     encryptedFileData(nullable: false) 
    } 

    static transients = ["fileName", "fileData"] 

    static belongsTo = [user: User] 
} 

SecureFileTests.groovy

import static org.junit.Assert.* 
import grails.test.mixin.* 
import grails.test.mixin.support.* 

import org.junit.* 

/** 
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions 
*/ 
@TestFor(SecureFile) 
class SecureFileTests { 

    static final String SAMPLE_PDF_FILE = "fileEncryptionTestSample.pdf" 

    void testConstraints() { 
     def samplePdfFile = new FileInputStream(SAMPLE_PDF_FILE) 

     // Not really encrypted for this mock. 
     def mockedSecureFile = new SecureFile(
       encryptedFileName: "--Not-Really-Encrypted--", 
       encryptedFileData: samplePdfFile.getBytes() 
       ) 
     mockForConstraintsTests(SecureFile, [mockedSecureFile]) 

     // Validation should fail if both properties are null. 
     def badMockedSecureFile = new SecureFile() 

     assert !badMockedSecureFile.validate() 
     assert "nullable" == badMockedSecureFile.errors["encryptedFileName"].code 
     assert "nullable" == badMockedSecureFile.errors["encryptedFileData"].code 
    } 
} 
+0

我只是想打個招呼,注意我刪除了'mockForConstraintTests'調用,它現在似乎正在工作。我感覺到這種感覺,我沒有在任何地方使用RTFM,這種行爲在單元測試中發生了變化,或者是其他事情正在發生? –

回答

0

移除badMockedSecureFile.errors代碼[ 「encryptedFileName」]。代碼。

你會得到你所期望的。

mockForConstraintsTests(SecureFile) 

// Validation should fail if both properties are null. 
def badMockedSecureFile = new SecureFile() 

assert !badMockedSecureFile.validate() 
assert "nullable" == badMockedSecureFile.errors["encryptedFileName"] 
assert "nullable" == badMockedSecureFile.errors["encryptedFileData"] 
+0

等等,什麼?爲什麼我會實例化一個'MyTest'類而不是'SecureFile'?此外,我添加了一個後續,我刪除了'mockForConstraintsTests'函數調用,現在它的作品。所以我想我的問題確實應該被編輯。 –

+0

對不起托馬斯。我使用MyTest類來檢查自己。我在這裏沒有更改爲SecureFile。現在看。試試這個,它會起作用。我認爲mockForConstraints必須提供用於限制約束條件。 – Visme

+0

沒有骰子。我必須刪除'mockForConstraintsTest'行。這是從Grails 2.2.4自動完成的嗎?如果我不調用'mockForConstraintsTest()',它可以在UnitTest中爲域類完美地工作。不幸的是,文檔沒有這樣顯示。 –