Groovy Spock:方法未被识别为调用
试图弄明白为什么Spock似乎不将(模拟对象的)方法调用识别为调用。看了一下文档()就想不出来了 下面是代码的简化版本:Groovy Spock:方法未被识别为调用,groovy,spock,Groovy,Spock,试图弄明白为什么Spock似乎不将(模拟对象的)方法调用识别为调用。看了一下文档()就想不出来了 下面是代码的简化版本: class VmExportTaskSplitter implements TaskSplitter<Export> { @Inject AssetServiceClient assetServiceClient @Override int splitAndSend(Export export) { Map b
class VmExportTaskSplitter implements TaskSplitter<Export> {
@Inject
AssetServiceClient assetServiceClient
@Override
int splitAndSend(Export export) {
Map batch = [:]
Map tags = [:]
if (true) {
println('test')
batch = assetServiceClient.getAssetIdBatch(export.containerUuid,
export.userUuid, (String) batch.scrollId, tags)
print('batch: ')
println(batch)
}
return 1
}
}
这里有一个恼人的部分:assetServiceClient.getAssetIdBatch
调用两边的两行都被打印出来。但斯波克声称没有任何调用
Using logging directory: './logs'
Using log file prefix: ''
test
batch: [assetIds:[1, 2, 3, 4, 5], scrollId:null]
Too few invocations for:
1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map) (0 invocations)
Unmatched invocations (ordered by similarity):
None
更改此行:
1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map) (0 invocations)
。。。关于:
1 * client.getAssetIdBatch(_ as String, _ as String, _, _ as Map)
在VmExportTaskSplitter
中,将空Map
传递到getAssetIdBatch
方法中,因此batch.scrollId
将为null
,并且它将与\uAS字符串不匹配
您的规范也可以简化,但这取决于您需要测试什么。仅当调用了getAssetIdBatch
方法时,才可以从您测试的then
部分进行猜测,这样编写就足够了:
def "tags should be parsed correctly"() {
setup:
Export export = new Export(containerUuid: "000", userUuid: "000", chunkSize: 10)
AssetServiceClient client = Mock(AssetServiceClient)
VmExportTaskSplitter splitter = new VmExportTaskSplitter()
splitter.assetServiceClient = client
when:
splitter.splitAndSend(export)
then:
1 * client.getAssetIdBatch('000', '000', null, [:])
}
非常感谢!这正是我想问的。不幸的是,真正的代码并不是完全固定的,如果你不介意的话,你能看看这个吗?
def "tags should be parsed correctly"() {
setup:
Export export = new Export(containerUuid: "000", userUuid: "000", chunkSize: 10)
AssetServiceClient client = Mock(AssetServiceClient)
VmExportTaskSplitter splitter = new VmExportTaskSplitter()
splitter.assetServiceClient = client
when:
splitter.splitAndSend(export)
then:
1 * client.getAssetIdBatch('000', '000', null, [:])
}