【问题标题】:Spock does not detect method invocationSpock 没有检测到方法调用
【发布时间】:2016-05-21 09:30:56
【问题描述】:

Spock 没有检测到 doTip 方法调用

(我需要共享一些“where”块。)

使用最新 groovyspock

为什么这段代码是错误的?

如何解决?

import spock.lang.Shared
import spock.lang.Specification

class Test extends Specification {
def controller
@Shared
String g = ""
@Shared
def tip = Mock(Tip)

def "test"() {
    controller = new TController(tip: tip)
    when:
    controller.transform(g)

    then:
    1 * tip.doTip(_)
}
}

class Tip {
def doTip(String f) {}
}

class TController {
Tip tip

def transform(String g) {
    tip.doTip(g)
}
}

【问题讨论】:

    标签: unit-testing testing groovy spock


    【解决方案1】:

    使用setup() 创建模拟如下所示:

    @Grab(group='org.spockframework', module='spock-core', version='1.0-groovy-2.4')
    
    import spock.lang.*
    
    class Test extends Specification {
        def controller
        @Shared String g = ""
        @Shared tip
    
        def setup() {
            tip = Mock(Tip)
        }
    
        def "test"() {
            given:
            controller = new TController(tip: tip)
    
            when:
            controller.transform(g)
    
            then:
            1 * tip.doTip(_)
        }
    }
    
    class Tip {
        def doTip(String f) {}
    }
    
    class TController {
        Tip tip
    
        def transform(String g) {
            tip.doTip(g)
        }
    }
    

    结果

    JUnit 4 Runner, Tests: 1, Failures: 0, Time: 78
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多