gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:03:30 31 4
gpt4 key购买 nike

我有一个静态方法的GroovyMock。当我的模拟方法被调用时,即使我接受了模拟的所有参数,由于未使用正确的参数,测试也会失败。为什么要这样?

// 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 * FileDownloadingService.download(*_) // or: (_, _, _)

关于grails - 为什么该Grails Spock GroovyMock测试失败(忽略参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635474/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com