我正在将现有应用程序从 Grails 2.4.4 升级到 Grails 4.0.10,我现在正在尝试测试适用于 Grails 2.4.4 应用程序的注销代码。但是,当我在删除该用户的所有令牌后调用 findAllByUsername
时,GORM 返回明显过时的数据。
为了测试这个,我试过
AuthenticationToken.list().find { it.username == username }
,它返回 null
。但是,AuthenticationToken.findAllByUsername(username)
返回刚刚删除的 3 条记录。
如何防止 findAllByUsername
在此单元测试中返回陈旧数据?
package us.cloudcard.api
import grails.testing.gorm.DataTest
import grails.testing.gorm.DomainUnitTest
import groovy.time.TimeCategory
import spock.lang.Specification
class AuthenticationTokenSpec extends Specification implements DomainUnitTest<AuthenticationToken>, DataTest {
def setup() {
[AuthenticationToken, Photo, Person].each { mockDomain(it) }
}
def "test logout"() {
setup:
String username = "bacon"
[username, "eggs", "biscuits", "gravy", username, "hashbrowns", username].each {
new AuthenticationToken(username: it).save(validate: false)
}
when:
List<AuthenticationToken> tokensForUser = AuthenticationToken.findAllByUsername(username)
AuthenticationToken.deleteAll(tokensForUser, flush: true)
then:
AuthenticationToken.list().find { it.username == username } == null // this passes
AuthenticationToken.findAllByUsername(username).empty // this fails
}
}