繁体   English   中英

如何在非域类上测试 Grails 验证

[英]How to test Grails validation on non-domain classes

我们将验证放在一个非域类上,当应用程序运行时它可以正常工作。 但是,我们想对验证进行单元测试,但该测试似乎忽略了约束块并仅使用默认验证约束。

这是我们的生产课:

import grails.validation.Validateable

@Validateable
class TransactToken {

    String institutionId
    String username
    boolean expired = false
    //optional fields
    String email
    String customCssUrl
    String roleName = Role.CARDHOLDER

    static constraints = {
        institutionId blank: false
        username blank: false
        expired notEqual: true
        email email: true, nullable: true
        customCssUrl url: true, nullable: true
        roleName inList: [ Role.DIRECTOR, Role.OFFICE, Role.CARDHOLDER]
    }

}

还有我们的测试类:

import spock.lang.Specification
import spock.lang.Unroll

class TransactTokenSpec extends Specification {

    @Unroll
    void "test isValid (institutionId: #institutionId, username: #username, expired: #expired, roleName: #roleName)"() {
        given: "A token with the provided params"
        TransactToken transactToken = new TransactToken(
                institutionId: institutionId,
                username: username,
                expired: expired,
                roleName: roleName,
                email: "bacon@eggs.edu",
                customCssUrl: "http://bacon.edu"
        )

        when:
        transactToken.validate()
        if (transactToken.hasErrors()) {println transactToken.errors}

        then:
        valid != transactToken.hasErrors()

        where:
        institutionId | username | expired | roleName        || valid
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER || true
        'asdf'        | ''       | false   | Role.CARDHOLDER || false
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER || true
        ''            | 'asdf'   | false   | Role.CARDHOLDER || false
        'asdf'        | ''       | false   | Role.CARDHOLDER || false
        'asdf'        | 'asdf'   | true    | Role.CARDHOLDER || false
        'asdf'        | 'asdf'   | false   | Role.OFFICE     || true
        'asdf'        | 'asdf'   | false   | Role.DIRECTOR   || true
        'asdf'        | 'asdf'   | false   | Role.ADMIN      || false

    }

}

所以,这有点像黑客,但我们想通了。

我认为问题在于我们需要 @TestFor 注释,我似乎不适用于非 grails 类。

依赖于这个类验证并使用 @TestFor 注释的服务,所以我们把这个测试移到那里,它按预期工作。

@TestFor(TransactTokenService)
@ConfineMetaClassChanges(Organization)
class TransactTokenServiceSpec extends Specification {

    @Unroll
    void "test #label: valid should be #valid."() {
        given: "A token with the provided params"
        TransactToken transactToken = new TransactToken(
                institutionId: institutionId,
                username: username,
                expired: expired,
                roleName: roleName,
                customCssUrl: url,
                email: email
        )

        when:
        transactToken.validate()

        then:
        valid != transactToken.hasErrors()

        where:
        institutionId | username | expired | roleName        | email              | url                  || valid || label
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER | "bacon@sharptop.co"| "http://sharptop.co" || true  || "all valid"
        'asdf'        | ''       | false   | Role.CARDHOLDER | ""                 | ""                   || false || "blank username"
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER | ""                 | ""                   || true  || "blank email and url"
        ''            | 'asdf'   | false   | Role.CARDHOLDER | ""                 | ""                   || false || "blank institution id"
        'asdf'        | 'asdf'   | true    | Role.CARDHOLDER | ""                 | ""                   || false || "expired token"
        'asdf'        | 'asdf'   | false   | Role.OFFICE     | ""                 | ""                   || true  || "valid office user"
        'asdf'        | 'asdf'   | false   | Role.DIRECTOR   | ""                 | ""                   || true  || "valid director user"
        'asdf'        | 'asdf'   | false   | Role.ADMIN      | ""                 | ""                   || false || "invalid admin user"
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER | "bacon@sharptop.co"| "bacon"              || false || "invalid url"
        'asdf'        | 'asdf'   | false   | Role.CARDHOLDER | "bacon"            | "http://sharptop.co" || false || "invalid email"
    }
}

仅供阅读 Grails 3 或 4 的任何人使用。Validateable 现在是一个接口而不是 Annotation。 你在一个类上像这样实现它:

import grails.validation.Validateable

class UserRow implements Validateable {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM