Groovy Spock:方法未被识别为调用

Groovy Spock:方法未被识别为调用,groovy,spock,Groovy,Spock,试图弄明白为什么Spock似乎不将(模拟对象的)方法调用识别为调用。看了一下文档()就想不出来了 下面是代码的简化版本: class VmExportTaskSplitter implements TaskSplitter<Export> { @Inject AssetServiceClient assetServiceClient @Override int splitAndSend(Export export) { Map b

试图弄明白为什么Spock似乎不将(模拟对象的)方法调用识别为调用。看了一下文档()就想不出来了

下面是代码的简化版本:

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, [:])
}