使用 use(TimeCategory) 时使用 Spock 进行单元测试失败

Unit testing with Spock fails when using use(TimeCategory)

我在我的项目中使用 Grails 2.4.4,我正在尝试使用 Spock 编写单元测试用例。我尝试进行单元测试的方法使用 use(TimeCategory)。测试用例 运行 的时间太长了,在调试时我发现它没有超过我在代码中 use(TimeCategory) 的那个点。请在下面找到我为其编写的方法和测试用例:

def getEstimatedSl(val) {

        // some code
        calculateSl()
        // some code
    }

def calculateSl() {
    use(TimeCategory) {
            interval = 10 
            startDate = //some date value// 
            endDate = //some date value// 
        } 

        while (endDate.compareTo(startDate) >= 0) { 
            use(TimeCategory) { 
                startDate += interval 
            } 
        }
}

void "test getEstimatedSl success"() {
        when:
        def dto = service.getEstimatedSl('ABCDEFGH')

        then:
        dto.slots ==  /*dto value that I get*/
        dto.count == 4
    }

有人可以告诉我我做错了什么吗?提前致谢。

很抱歉,我发布答案的时间晚了,但事实证明我服务中的代码没有任何问题。我没有在我的测试用例中设置时间间隔(第 10 行),因此我的 while 进入了无限循环。 无论如何,感谢您的帮助,伙计们!