【发布时间】:2020-11-08 17:15:37
【问题描述】:
以下 Spock 测试未能计算对 Mock 方法的调用:
def setup() {
mojo = new PactCreateVersionTagMojo()
mojo.pactBrokerUrl = 'http://broker:1234'
mojo.pacticipant = 'test'
mojo.pacticipantVersion = '1234'
mojo.tag = 'testTag'
}
def 'calls pact broker client with mandatory arguments'() {
given:
mojo.brokerClient = Mock(PactBrokerClient)
when:
mojo.execute()
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.createVersionTag(
'test', '1234', 'testTag')
}
你可以找到它here。
除去参数验证码的 SUT 代码是:
class PactCreateVersionTagMojo : PactBaseMojo() {
override fun execute() {
...
createVersionTag()
}
private fun createVersionTag() =
brokerClient!!.createVersionTag(pacticipant!!, pacticipantVersion.orEmpty(), tag.orEmpty())
你可以找到它here。
错误如下:
我在同一个项目上有一个非常相似的例子,通过就好了:
def 'passes optional parameters to the pact broker client'() {
given:
mojo.latest = 'true'
mojo.to = 'prod'
mojo.brokerClient = Mock(PactBrokerClient)
when:
mojo.execute()
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234',
new Latest.UseLatest(true), 'prod') >> new CanIDeployResult(true, '', '')
}
override fun execute() {
...
val result = brokerClient!!.canIDeploy(pacticipant!!, pacticipantVersion.orEmpty(), latest, to)
}
我已经调查了测试期间发生的呼叫,它似乎符合预期。 此外,我尝试使用通配符参数约束创建验证,但它仍然不起作用。
在我看来,我的测试配置错误,但我无法发现通过的测试和失败的测试之间的区别。
【问题讨论】:
标签: unit-testing kotlin groovy tdd spock