首页 > 解决方案 > spock 在这个测试中是如何调用这个函数的?

问题描述

我正在浏览这个示例,但它的某些内容让我非常困惑:https ://www.testcookbook.com/book/groovy/jenkins/intro-testing-job-dsl.html

在这个测试中,如何/执行getJobFiles()什么?我没有看到它在任何地方被调用。有什么魔法jobFiles吗?指定jobFiles以某种方式调用 getJobFiles?

import javaposse.jobdsl.dsl.DslScriptLoader
import javaposse.jobdsl.plugin.JenkinsJobManagement
import org.junit.ClassRule
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class JobScriptsSpec extends Specification {
    @Shared
    @ClassRule
    JenkinsRule jenkinsRule = new JenkinsRule()

    @Unroll
    def 'test script #file.name'(File file) {
        given:
        def jobManagement = new JenkinsJobManagement(System.out, [:], new File('.'))

        when:
        new DslScriptLoader(jobManagement).runScript(file.text)

        then:
        noExceptionThrown()

        where:
        file << jobFiles
    }

    static List<File> getJobFiles() {
        List<File> files = []
        new File('jobs').eachFileRecurse {
            if (it.name.endsWith('.groovy')) {
                files << it
            }
        }
        files
    }
}

编辑

似乎 jobFiles 确实调用了 getJobFiles() 但我不明白如何。这是 groovy 还是 spock 功能?我一直在尝试对此进行研究,但可以找到任何详细解释这一点的东西。

标签: groovyspockjenkins-groovy

解决方案


编辑

当 Groovy 确定jobFiles不引用任何现有变量时,它会将名称视为 Groovy属性,然后允许它利用快捷表示法来访问属性。


推荐阅读