为什么Grails Spock GroovyMock测试失败(参数被忽略)?

时间:2014-12-24 10:32:18

标签: grails mocking spock

我有一个静态方法的GroovyMock。当我的mock方法被调用时,测试失败,因为没有使用正确的参数,即使我接受了mock的所有参数。为什么这样呢?

// FileDownloadingService.groovy
class FileDownloadingService {

    // I am going to mock this static method
    static void download(URL urlLocation, String localDir, String localName) {
    }
}


// ServiceUnderTestService.groovy
class ServiceUnderTestService {
    def downloadData(URL url) {
        FileDownloadingService.download(url, "temp", "ReferenceData.gz")

    }
}


// within ServiceUnderTestServiceSpec
void "file is downloaded"() {
    given: "A url for the file to download"
    def urlLocation = "http://example.com/ReferenceData.gz"
    def url = new URL(urlLocation)
    def fileDownloadMock = GroovyMock(FileDownloadingService, global: true)

    when: "we call downloadData"
    service.downloadData(url)

    then: "we actually try to download it"
    1 * fileDownloadMock.download(_, _, _)
}

我收到以下错误消息:

|  Too few invocations for:
1 * fileDownloadMock.download(_, _, _)   (0 invocations)
Unmatched invocations (ordered by similarity):
1 * fileDownloadMock.download(http://example.com/ReferenceData.gz, 'temp', 'ReferenceData.gz')
    at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
    at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)

为什么模拟没有通过,因为我指定所有参数都有效,没有?

1 个答案:

答案 0 :(得分:0)

对于静态方法,需要按如下方式指定交互:

1 * FileDownloadingService.download(*_) // or: (_, _, _)
相关问题