为此,您必须深入了解 Spock 的工作原理,这很神奇。
制作类似的东西
def "mock example"() {
given:
Receiver receiver = Mock()
def producer = new Producer(receiver)
when:
producer.createItem()
then:
1 * receiver.receive(_) >> true
}
工作,它将所有模拟交互声明从 then 块直接移到 when 块代码前面。
这就是代码在 AST 转换后的样子。
@org.spockframework.runtime.model.FeatureMetadata(name = 'mock example', ordinal = 2, line = 43, blocks = [org.spockframework.runtime.model.BlockKind.SETUP[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.WHEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.THEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41], parameterNames = [])
public void $spock_feature_0_2() {
Receiver receiver = this.MockImpl('receiver', Receiver)
java.lang.Object producer = new Producer(receiver)
this.getSpecificationContext().getMockController().enterScope()
this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(52, 9, '1 * receiver.receive(_) >> true').setFixedCount(1).addEqualTarget(receiver).addEqualMethodName('receive').setArgListKind(true, false).addEqualArg(_).addConstantResponse(true).build())
producer.createItem()
this.getSpecificationContext().getMockController().leaveScope()
this.getSpecificationContext().getMockController().leaveScope()
}
如果我们获取您的代码,那么我们可以看到同样的事情发生了。
@org.spockframework.runtime.model.FeatureMetadata(name = 'non-working condition check', ordinal = 0, line = 5, blocks = [org.spockframework.runtime.model.BlockKind.SETUP[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.WHEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.THEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41], parameterNames = [])
public void $spock_feature_0_0() {
org.spockframework.runtime.ErrorCollector $spock_errorCollector = org.spockframework.runtime.ErrorRethrower.INSTANCE
org.spockframework.runtime.ValueRecorder $spock_valueRecorder = new org.spockframework.runtime.ValueRecorder()
java.lang.Object result = 0
this.getSpecificationContext().getMockController().enterScope()
then:
if (true) {
try {
org.spockframework.runtime.SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'result == 1', 16, 24, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), result) == $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), 1)))
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'result == 1', 16, 24, null, $spock_condition_throwable)}
finally {
}
} else {
[this.MockImpl(null, null, groovy.lang.Closure)].each({
return this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(20, 21, 'it.call() >> ""').addEqualTarget(it).addEqualMethodName('call').setArgListKind(true, false).addConstantResponse('').build())
})
}
java.lang.System.out.println("----- before: $result")
result = 1
java.lang.System.out.println("----- after: $result")
this.getSpecificationContext().getMockController().leaveScope()
this.getSpecificationContext().getMockController().leaveScope()
}
@org.spockframework.runtime.model.FeatureMetadata(name = 'working condition check', ordinal = 1, line = 25, blocks = [org.spockframework.runtime.model.BlockKind.SETUP[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.WHEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41, org.spockframework.runtime.model.BlockKind.THEN[]org.codehaus.groovy.ast.AnnotationNode@d9f41], parameterNames = [])
public void $spock_feature_0_1() {
org.spockframework.runtime.ErrorCollector $spock_errorCollector = org.spockframework.runtime.ErrorRethrower.INSTANCE
org.spockframework.runtime.ValueRecorder $spock_valueRecorder = new org.spockframework.runtime.ValueRecorder()
java.lang.Object result = 0
java.lang.System.out.println("----- before: $result")
result = 1
java.lang.System.out.println("----- after: $result")
then:
if (true) {
try {
org.spockframework.runtime.SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'result == 1', 36, 24, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), result) == $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), 1)))
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'result == 1', 36, 24, null, $spock_condition_throwable)}
finally {
}
}
this.getSpecificationContext().getMockController().leaveScope()
}