我们对非域类进行验证,该类在应用程序运行时工作正常。但是,我们想对验证进行单元测试,但测试似乎忽略了约束 block 并仅使用默认验证约束。
这是我们的生产类:
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: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf9faf8f4f5dbfefcfce8b5feffee" rel="noreferrer noopener nofollow">[email protected]</a>",
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 | "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8eecefede1e0cefde6effcfefae1fea0ede1" rel="noreferrer noopener nofollow">[email protected]</a>"| "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 | "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="214340424e4f615249405351554e510f424e" rel="noreferrer noopener nofollow">[email protected]</a>"| "bacon" || false || "invalid url"
'asdf' | 'asdf' | false | Role.CARDHOLDER | "bacon" | "http://sharptop.co" || false || "invalid email"
}
}
关于validation - 如何测试非域类上的 Grails 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34745297/