【问题标题】:validate() Mock Constraint Testing in GrailsGrails 中的 validate() 模拟约束测试
【发布时间】:2013-09-01 01:45:19
【问题描述】:

我有一个单元测试,我正在尝试让验证成功。

基本上,当我调用badMockedSecureFile.validate() 时,它并没有达到我的预期,这就是encryptedFileNameencryptedFileData 这两个字段的验证失败。

当我中断调试器时,我只是在随后的断言中为badMockedSecureFile.errors 获得了一个null 值。这是我的两个文件:

任何意见将不胜感激。我找不到完全相同的问题。我将 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
    }
}

【问题讨论】:

  • 我只是想插话并注意我删除了mockForConstraintTests 调用,它现在似乎正在工作。我有这种感觉,我没有在某个地方进行 RTFM,并且这种行为在单元测试中发生了变化,还是发生了其他事情?

标签: unit-testing grails constraints


【解决方案1】:

从 badMockedSecureFile.errors["encryptedFileName"].code 中删除代码。

你会得到你所期望的。

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"]

【讨论】:

  • 等等,什么?为什么我要实例化MyTest 类而不是SecureFile?我还添加了一个后续操作,我删除了 mockForConstraintsTests 函数调用,它现在可以工作了。所以我想我的问题真的应该被编辑。
  • 对不起托马斯。我用来检查自己的 MyTest 类。我没有在这里更改为 SecureFile。现在看看。现在试试这个。它会工作的。我认为 mockForConstraints 必须验证约束。
  • 没有骰子。我必须删除 mockForConstraintsTest 行。这是从 Grails 2.2.4 开始自动完成的吗?如果我不调用mockForConstraintsTest(),它在域类的单元测试中工作得非常好。不幸的是,文档没有这样显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多