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

标签 grails groovy spock

我在项目中使用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行),因此我的时间处于无限循环中。
谢谢大家的帮助!

关于grails - 使用use(TimeCategory)时,使用Spock进行单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979981/

相关文章:

testing - Geb和Spock作为集成测试框架

grails - Grails可搜索返回唯一或不同的结果

groovy - 无法从模型模板呈现日期

groovy - 如何配置 GroovyConsole 以便我不必在启动时导入库?

arrays - 将元素添加到Grails中的数组

groovy - 史波克行为怪异

grails - 使用log4j捕获空的日志文件

json - 获取JSON中每个字段的名称

unit-testing - 了解Grails的mockFor需求

具有许多私有(private)方法的 Java 测试类