2014-12-24 81 views
0

我有一個靜態方法的GroovyMock。當我的模擬方法被調用時,測試失敗,因爲沒有使用正確的參數,即使我接受模擬的所有參數。這是爲什麼呢?爲什麼Grails Spock 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) 

爲什麼模擬不通過,因爲我指定所有參數都是有效的,不是嗎?

回答

0

對於靜態方法,相互作用的,需要指定如下:

1 * FileDownloadingService.download(*_) // or: (_, _, _) 
+0

完美,謝謝。 – John