gpt4 book ai didi

unit-testing - Grails的正确Spock语法是什么?

转载 作者:行者123 更新时间:2023-12-02 15:05:41 24 4
gpt4 key购买 nike

我正在运行Grails 2.5.0应用程序,并且执行以下测试:

package moduleextractor

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
* See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
@TestFor(ExtractorController)
class ExtractorControllerSpec extends Specification {

def moduleDataService
def mockFile

def setup() {
moduleDataService = Mock(ModuleDataService)
mockFile = Mock(File)
}

def cleanup() {
}

void "calls the moduleDataService"() {
given: 'a term is passed'
params.termCode = termCode
when: 'the getModuleData action is called'
controller.getModuleData()
then: 'the service is called 1 time'
1 * moduleDataService.getDataFile(termCode, 'json') >> mockFile
where:
termCode = "201415"
}
}

如果我运行 grails test-app unit:spock,我会得到:
| Tests PASSED - view reports in /home/foo/Projects/moduleExtractor/target/test-reports

我不明白为什么会看到2个测试。我的BuildConfig文件中没有包含spock,因为Grails 2.5.0中已经包含了spock。另外,由于我还没有服务,因此该测试不应该通过。为什么会通过?

另外,当我运行此 grails test-app ExtractorController时,我得到另一个结果:
| Running 2 unit tests...
| Running 2 unit tests... 1 of 2
| Failure: calls the moduleDataService(moduleextractor.ExtractorControllerSpec)
| Too few invocations for:
1 * moduleDataService.getDataFile(termCode, 'json') >> mockFile (0 invocations)
Unmatched invocations (ordered by similarity):
None
at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at moduleextractor.ExtractorControllerSpec.calls the moduleDataService(ExtractorControllerSpec.groovy:27)
| Completed 1 unit test, 1 failed in 0m 3s
| Tests FAILED - view reports in /home/foo/Projects/moduleExtractor/target/test-reports
| Error Forked Grails VM exited with error

如果我运行 grails test-app unit:我得到:
| Running 4 unit tests...
| Running 4 unit tests... 1 of 4
| Failure: calls the moduleDataService(moduleextractor.ExtractorControllerSpec)
| Too few invocations for:
1 * moduleDataService.getDataFile(termCode, 'json') >> mockFile (0 invocations)
Unmatched invocations (ordered by similarity):
None
at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at moduleextractor.ExtractorControllerSpec.calls the moduleDataService(ExtractorControllerSpec.groovy:27)
| Completed 1 unit test, 1 failed in 0m 3s
| Tests FAILED - view reports in /home/foo/Projects/moduleExtractor/target/test-reports
| Error Forked Grails VM exited with error

首先有人可以告诉我运行spock测试的正确语法是什么?

另外,在命令中包含 unitunit:unit:spock有什么区别?

(由于Spock随Grails 2.5.0一起提供,因此无论如何它将运行spocks测试。)

正确的语法是什么,为什么它看到2个测试而不是1个?

最佳答案

不必担心测试数量。对我来说从来都不是问题。您始终可以检查报告HTML文件,以查看确切的内容。

我总是与任何一个进行测试

grails test-app

要么
grails test-app ExtractorController

您得到的错误意味着您对测试进行了编码,以期望在调用controller.getModuleData()时使用参数null和'json'调用moduleDataService.getDataFile()。但是,从未调用moduleDataService.getDataFile(),因此测试失败。

Spock需要一些习惯。我建议查看Grails文档中的示例并阅读 Spock Framework Reference

关于unit-testing - Grails的正确Spock语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859507/

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