简体   繁体   English

Spock:方法未被识别为调用

[英]Spock: method not recognized as an invocation

Trying to figure out why Spock doesn't seem to recognize a method call (of a Mocked object) as an invocation.试图弄清楚为什么 Spock 似乎没有将(模拟对象的)方法调用识别为调用。 Looked at the docs ( http://spockframework.org/spock/docs/1.1-rc-3/all_in_one.html#_mocking ) and couldn't figure it out.查看文档( http://spockframework.org/spock/docs/1.1-rc-3/all_in_one.html#_mocking )并无法弄清楚。

Here's a dumbed down version of the code:这是代码的简化版本:

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

    }
}

And now the test:现在测试:

class VmExportTaskSplitterSpecification extends Specification{
    def "tags should be parsed correctly"(){
        setup:
        Export export = new Export(containerUuid: "000", userUuid: "000", chunkSize: 10)
        AssetServiceClient client = Mock(AssetServiceClientImpl)
        VmExportTaskSplitter splitter = new VmExportTaskSplitter()
        splitter.assetServiceClient = client
        Map map1 = [assetIds:["1","2","3","4","5"],scrollId:null]
        client.getAssetIdBatch(_ as String,_ as String, null, _ as Map) >> map1

        when:
        splitter.splitAndSend(export)

        then:
        1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map)
    }
}

Here's the annoying part: both lines on either side of the assetServiceClient.getAssetIdBatch call are printed.这是烦人的部分:在assetServiceClient.getAssetIdBatch调用两侧的两assetServiceClient.getAssetIdBatch被打印出来。 But Spock is claiming there are no invocations whatsoever...但是 Spock 声称没有任何调用......

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

Change this line:改变这一行:

1 * client.getAssetIdBatch(_ as String, _ as String, _ as String, _ as Map)   (0 invocations)

... on: ... 在:

1 * client.getAssetIdBatch(_ as String, _ as String, _, _ as Map)

In VmExportTaskSplitter you pass empty Map into getAssetIdBatch method so batch.scrollId will be null and it will not match the _ as String .VmExportTaskSplitter您将空Map传递给getAssetIdBatch方法,因此batch.scrollId将为null并且它不会匹配_ as String


Your specification can be also simplified, but it depends on what do you need to test.您的规范也可以简化,但这取决于您需要测试什么。 Guessing from the then part you test only if the getAssetIdBatch method was called then it is enough to write it like this:then部分猜测您仅在getAssetIdBatch方法时进行测试,然后像这样编写它就足够了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM