我正在编写Jenkins管道库,并且在模拟/验证现有Jenkins管道步骤时遇到一些困难。
我正在使用jenkins-spock by homeaway进行单元测试,但我认为我的问题与Spock有关。
customerBindingSource
这是我的单元测试。我试图说的是writeFile是与匹配logFileText的内容的文本一起调用的,而忽略了其他参数。我尝试了多种组合,但是似乎总是能得到以下响应:
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import com.company.pipeline.providers.BuildLogProvider
class PublishBuildLogSpec extends JenkinsPipelineSpecification {
BuildLogProvider buildLogProvider = Mock()
PublishBuildLog publishBuildLog
def setup () {
publishBuildLog = new PublishBuildLog(buildLogProvider: buildLogProvider)
explicitlyMockPipelineStep('writeFile')
}
def "Gets the log file contents for a specific job and build"() {
when:
"the call method is executed with the jobName and buildNumber parameters set"
publishBuildLog.call("JOBNAME", "42")
then:
"the getBuildLog on the buildLogProvider is called with those parameters"
1 * buildLogProvider.getBuildLog("JOBNAME", "42")
}
def "the contents of log file is written to the workspace"() {
given:
"getBuildLog returns specific contents"
def logFileText = "Example Log File Text"
buildLogProvider.getBuildLog(_, _) >> logFileText
when:
"publishBuildLog.call is executed"
publishBuildLog.call(_, _)
then:
"the specific contents is passed to the writeFile step"
1 * getPipelineMock("writeFile").call([file: _ , text: logFileText])
}
}
这是为了测试此类
Too few invocations for:
1 * getPipelineMock("writeFile").call([file: _ , text: "Example Log File Text"]) (0 invocations)
Unmatched invocations (ordered by similarity):
1 * (explicit) getPipelineMock("writeFile").call(['file':'filename', 'text':'Example Log File Text'])
我对如何验证此电话一无所知。我在Java和Junit方面有很多经验,但是我对Spock还是比较陌生。
我该如何验证?
答案 0 :(得分:0)
对我来说,您的考试通过了。但是有一件我觉得很奇怪的事情:您在when:
块中使用小丑,您应该像第一个功能方法中一样真正使用具体参数:
when: "publishBuildLog.call is executed"
publishBuildLog.call(_, _)
您应该写:
when: "publishBuildLog.call is executed"
publishBuildLog.call("JOBNAME", "42")
对我来说,如果我将其用作虚拟类以使代码编译(因为您未提供源代码),则此方法很好:
class BuildLogProvider {
def getBuildLog(def jobName, def buildNumber) {}
}