我有一个单元测试,我试图让验证成功。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,这种行为在单元测试中发生了变化,或者是其他事情正在发生? –