陈旧的 GORM 会话导致 Spock 单元测试失败

时间:2021-06-25 17:53:09

标签: grails grails-orm spock

我正在将现有应用程序从 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
    }
}

0 个答案:

没有答案