带闭包的 Groovy Spock 单元测试

标签 groovy jenkins-plugins spock

如何在 Spock/groovy 中执行此操作?

package org.jenkinsci.plugins

import hudson.matrix.*
import spock.lang.*
import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule

class xxxx extends Specification {

    @Rule JenkinsRule rule = new JenkinsRule()

    def 'matrix'() {
        given:
        def matrixProject = rule.createMatrixProject()
        AxisList axl = new AxisList();
        def axis = new TextAxis('TEST', "1", "2", "3")

        axl.add(axis)
        matrixProject.setAxes(axl)

        expect: matrixProject.scheduleBuild2(0).get().logFile.text.contains("Some String!")

        matrixProject.scheduleBuild2(0).get().getRuns().each(){
            expect: it.logFile.text.contains("Another String")
        }
    }
}

具体来说,如何使用嵌套测试运行闭包? “另一个字符串”测试不起作用

最佳答案

这有效吗?

def 'matrix'() {
    given:
        def matrixProject = rule.createMatrixProject()
        def axis = new TextAxis('TEST', "1", "2", "3")
        matrixProject.axes.add(axis)

    expect:
        with( matrixProject.scheduleBuild2(0).get() ) {
            logFile.text.contains("Some String!")
            runs.every { it.logFile.text.contains("Another String") }
        }
    }
}

关于带闭包的 Groovy Spock 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19425678/

相关文章:

html - 如何在gsp页面上使用变量值作为HTML元素的ID?

css - 在我的 css 中添加文本并将其显示在 html 页面电子邮件中

jenkins - 在 Jenkins 上重新运行失败测试的插件

java - 从同一目录中的文件读取

Grails 无法获取 null 对象的属性 'id'

grails - 具有连字符名称的Grails插件的命名约定

jenkins - 使用 Spock 的 Groovy 共享库测试流水线步骤方法

testing - 无需设置即可运行特定的 Spock 测试?

jdbc - 使用groovy Sql批量插入?

git - 使用来自 Git 的代码运行 Jenkins 流水线