我们将验证放在非域类上,在运行应用程序时可以正常运行。但是,我们希望对验证进行单元测试,但测试似乎忽略了约束块并仅使用默认验证约束。
这是我们的生产类:
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
}
}
答案 0 :(得分:2)
所以,这有点像黑客,但我们想出来了。
我认为问题在于我们需要@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"
}
}
答案 1 :(得分:0)
供任何人在Grails 3或4上阅读本文时,都可以参考。现在,Validateable是一个接口,而不是Annotation。您可以在一个类上实现它:
import grails.validation.Validateable
class UserRow implements Validateable {
...
}