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

标签 unit-testing grails spock

我正在运行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/

相关文章:

android retrofit2, dagger2 单元测试

c# - 测试使用 HttpContext.Current.Request.Files 的 Web API 方法?

c# - 在抽象通用测试类中定义单元测试

forms - 如何在 grails 应用程序中进行正确的验证和重定向

data-binding - Grails 将请求参数绑定(bind)到枚举

python - 为什么 (3.3==np.asarray([3.3])) 等于 [True] 而不是 False?

spring - 在 Grails 中动态设置消息 i18n

java - 从 .class 文件恢复已删除的 Spock 测试用例

grails - 如何测试具有 Basecontroller 的 @Mixin 的 Controller

java - 简单的 Mockito 验证在 JUnit 中有效但在 Spock 中无效