2013-09-01 76 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中为域类完美地工作。不幸的是,文档没有这样显示。 –