【发布时间】:2015-07-29 21:45:22
【问题描述】:
我有一个 Token 实体:
class Token {
/**
* Constants for various namespaces
*/
public static final String NS_PASSWORD_RESET = "pass-reset";
/**
* A simple string unlimited in content that defines a scope of the token
*/
String namespace
/**
* This fields holds an identifier for anything specific that the process might need
*/
Long identifier
/**
* The actual token itself
*/
String token
Timestamp dateCreated
Timestamp lastUpdated
Timestamp expiration
static mapping = {
autoTimestamp true
}
static constraints = {
}
}
以及具有以下方法的服务类:
def creareNewToken(String ns, int timeout) {
def token = new Token()
token.setNamespace(ns)
token.setToken(this.generateToken(15))
//persist the object
token.save(flush: true)
return token
}
我为服务类创建了一个集成测试:
class TokenServiceIntegrationSpec extends IntegrationSpec {
TokenService tokenService
def "test creareNewToken"() {
when:
def token = tokenService.creareNewToken(Token.NS_PASSWORD_RESET, 60)
then:
token instanceof Token
token.getNamespace() == Token.NS_PASSWORD_RESET
token.getToken().length() == 15
token.getDateCreated() == ''
}
}
当我执行测试时,我得到:
Failure: test
creareNewToken(com.iibs.security.TokenServiceIntegrationSpec)
| Condition not satisfied:
token.getDateCreated() == ''
| | |
| null false
com.iibs.security.Token : (unsaved)
at com.iibs.security.TokenServiceIntegrationSpec.test creareNewToken(TokenServiceIntegrationSpec.groovy:31)
这个问题的原因可能是什么?似乎对象实际上没有保存,并且粗略地说,dateCreated 也没有填充。我的问题是为什么它没有保存?我有许多其他以类似方式构建的测试,它们可以正常工作。
感谢您的建议。
【问题讨论】:
标签: grails groovy grails-orm integration-testing spock