我有一個單元測試,我試圖讓驗證成功。validate()模擬約束在Grails中測試
基本上,當我打電話給badMockedSecureFile.validate()
時,它沒有達到我期望的水平,而且這兩個字段encryptedFileName
和encryptedFileData
的驗證失敗。
當我在調試器中斷時,我只是在隨後的斷言中得到badMockedSecureFile.errors
的null
值。這裏是我的兩個文件:
任何輸入將不勝感激。我找不到一個確切的類似問題。我正在使用grails 2.2.4與Oracle 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
}
}
我只是想打個招呼,注意我刪除了'mockForConstraintTests'調用,它現在似乎正在工作。我感覺到這種感覺,我沒有在任何地方使用RTFM,這種行爲在單元測試中發生了變化,或者是其他事情正在發生? –