【问题标题】:Grails unit testing custom codec using Spock使用 Spock 对自定义编解码器进行 Grails 单元测试
【发布时间】:2014-06-09 16:32:51
【问题描述】:

我想创建一个 Spock 单元测试来测试我创建的自定义编解码器。我看到的大多数测试自定义编解码器的示例都扩展了 GrailsUnitTestCase 来执行此操作。有人可以为我指出如何使用 Spock 执行此操作的正确方向吗?

【问题讨论】:

  • 你必须扩展规范,比如@TestFor(ImageService) class ImageServiceSpec extends Specification { def setup() { } def cleanup() { } void "test something"() { } }

标签: unit-testing grails spock


【解决方案1】:

我最终做了以下事情:

@TestMixin(GrailsUnitTestMixin)
class SecureCodecSpec extends Specification {
    def setup() {
        grailsApplication.config.acme.encryption.password = 'topsecret'
        mockCodec(SecureCodec)
    }

    @Unroll
    def "SecureCodec with string value #original encodes and decodes properly"() {
        when:
        def encoded = original.encodeAsSecure()
        def decoded = encoded.decodeSecure()

        then:
        original != encoded
        encoded != decoded
        original == decoded

        where:
        original | _
        'secret' | _
        ''       | _
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2013-09-24
    • 2014-06-21
    • 2016-06-17
    相关资源
    最近更新 更多