当我在 Grails 中运行这段代码时,它工作得很好。
String getLoginToken() {
generatePassword()
passwordExpired = false
[email, password].join(',').encodeAsBase64()
}
但是,这个 Spock 测试失败了
def "test getLoginToken"() {
setup:
String email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a686b6965644a6f6d6d79246f6e7f" rel="noreferrer noopener nofollow">[email protected]</a>"
Person person = new Person(email: email)
when:
String token = person.getLoginToken()
then:
token.decodeBase64() == "$email,$person.password"
}
有以下异常(exception)情况
| Failure: test getLoginToken(com.campuscardtools.myphotoid.PersonSpec)
| groovy.lang.MissingMethodException: No signature of method: java.lang.String.encodeAsBase64() is applicable for argument types: () values: []
Possible solutions: decodeBase64()
at com.campuscardtools.myphotoid.Person$$EPFScS6i.getLoginToken(Person.groovy:140)
at com.campuscardtools.myphotoid.PersonSpec.test getLoginToken(PersonSpec.groovy:68)
我的理解是Groovy在String类上提供了encodeAsBase64()(参见: http://mrhaki.blogspot.com/2009/11/groovy-goodness-base64-encoding.html ),那么为什么这在单元测试中不起作用?
最佳答案
而不是
"Blah".encodeAsBase64()
你需要
"Blah".encodeBase64()
没有“As”
关于unit-testing - Grails String.encodeAsBase64() 在 Spock 测试中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815250/