【问题标题】:Spock check number of methods call for private objectSpock 检查调用私有对象的方法数量
【发布时间】:2014-03-20 13:21:17
【问题描述】:

我为 REST 调用 3rd 方 api 创建了一个服务。有一个 RestBuilder 实例:

class ThirdPartyApiService{
    ...    
    private def restClient = new RestBuilder()
    ...
}

在测试中,我想检查 restClient.post(...) 被调用了多少次。尝试使用间谍,但没有成功。

class ThirdPartyApiServiceIntegrationSpec extends IntegrationSpec {

    def thirdPartyApiService

    def "test smthing"() {
        setup: "prepare spies"
        def restBuilderSpy = GroovySpy(RestBuilder, global: true)

        when:
        thirdPartyApiService.someMethod()

        then:
        1 * restBuilderSpy.post(* _)
    }
}

它失败了
|调用太少:
1 * restBuilderSpy.post(*_)(0 次调用)
我错过了什么吗?如何实现我的目标?

【问题讨论】:

    标签: grails testing spock


    【解决方案1】:

    您只是在测试中遗漏了一行漂亮的代码。 ;)

    setup: "prepare spies"
        def restBuilderSpy = GroovySpy(RestBuilder, global: true)
        thirdPartyApiService.restClient = restBuilderSpy
    

    【讨论】:

    • 不行,如何处理私有字段?如果在 global:true 参数中使用你的建议,则不需要
    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2019-06-06
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多