单元测试服务中的函数而不实际调用该函数

时间:2014-03-13 03:19:15

标签: unit-testing spock

我有一个基本的单元测试,它构建一个对象并将其传递给服务方法。然后该方法检查图像类型并调用convertToDicomService.async(uploadedFile)函数。我希望我的单元测试只测试该代码块是否正常工作而不调用其他函数。

def "sync uploaded file"() {
    given:
    def  eventRouterService = Mock(EventRouterService)
    UploadedFile uploadedFile = UploadedFile.build()
    uploadedFile.uploadedFileType = UploadedFileType.DICOM

    when:
    service.handlePossiblyReadyToSend(uploadedFile)

    then:
    1 * eventRouterService.handleFileReadyToSend(uploadedFile)
}

这是服务方法

def handlePossiblyReadyToSend(UploadedFile uploadedFile) {
    log.trace("Determining if file is READY_TO_SEND: $uploadedFile")
    switch (uploadedFile.uploadedFileType) {
        case UploadedFileType.JPEG:
            convertToDicomService.async(uploadedFile)
            break
        case UploadedFileType.DICOM:
            handleFileReadyToSend(uploadedFile)
            break
        default:
            // Bad news bears: We don't know this file type! (should not happen, since it's an enum)
            log.error("Impossible uploaded file type found: $uploadedFile")
            break
    }
}

此外,我对单元测试有点新,并且对任何相关建议持开放态度

0 个答案:

没有答案