Spock spy注册Groovy方法的调用太少(没有) [英] Spock spy registers too few invocations (none) of Groovy method

查看:741
本文介绍了Spock spy注册Groovy方法的调用太少(没有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Groovy扩展库,它为List类增加了额外的方法。我正在尝试使用Spock编写测试,但我无法获得一些测试结果。



代码



有许多方法都具有相同的形式。我添加了与 tail()(或 head() first()等),但是如果List是空的,那么只会返回 null ,而不是抛出Exception。下面显示了一个示例(为解决问题添加了println()):

  public static def tailIfAny(List list){

printlntailIfAny()list.size():$ {list.size()}

def r = list.size()? list.tail():null
printlnr:$ r
return r
}

我想对 tailIfAny()进行两个测试。第一个测试应检查是否在空列表上调用该方法时不会抛出异常。这很简单,很有效。我想写的另一个测试是检查 tail()是否被调用,如果列表不为空。



测试



这是我的测试,用于检查是否调用 tail()如果List不为空:

  deftailIfAny()calls tail()(){

setup:
def list = Spy(ArrayList)
list<< 'a'
列表<< 'b'

当:
printlnlist $ {list} - $ {list.size()}
println之前tailIfAny()
list.tailIfAny()
printlnafter tailIfAny()

then:
1 * list.tail()
}

错误信息



测试我得到这个失败的消息:

太少的调用:

1 * list.tail()(0 invocations)

不匹配的调用(按相似性排序):

1 * list.toArray()
1 * list.iterator()
3 * list.size ()
1 * list.isEmpty()


at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org .spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at com.fgi.extensions.ListExtensionsTests.add setup(ListExtensionsTests.groovy:65)中的元素

但是从stdout输出(下面)我可以看到th应该已经调用 tail()



测试标准
$ b

 list [a,b]  -  2 
在tailIfAny()之前
tailIfAny()list.size():2
r:[b] //这告诉我tail()正在被调用。
之后tailIfAny()

任何人都可以阐明为什么Spock认为 tail()没有被调用?或者在这里发生了什么我缺少的东西?



谢谢。

从技术上讲, tail()不在列表对象上调用( ArrayList 不会声明这样的方法)。因此,基于代理的 Spy()没有机会拦截该呼叫。尝试使用 GroovySpy()


I have written a Groovy extension library that adds extra methods to the List class. I am trying to write tests for this using Spock but I am unable to get some of my tests to work.

The code

There are a number of methods that all have the same form. I have added methods that work the same as tail() (or head(), first(), etc.) but that will just return null if the List is empty, rather than throwing an Exception. An example is shown below (println()s added for troubleshooting):

public static def tailIfAny(List list) {

    println "tailIfAny() list.size(): ${list.size()}"

    def r = list.size() ? list.tail() : null
    println "r: $r"
    return r
}

I want to have two tests for tailIfAny(). The first test should check that no exception is thrown if you call the method on an empty List. This was straightforward and works. The other test I want to write is to check that tail() is called if the list is not empty.

The test

This is my test to check that tail() is called if the List is not empty:

def "tailIfAny() calls tail()"() {

    setup:
    def list = Spy(ArrayList)
    list << 'a'
    list << 'b'

    when:
    println "list ${list} -- ${list.size()}"
    println "Before tailIfAny()"
    list.tailIfAny()
    println "After tailIfAny()"

    then:
    1 * list.tail()
}

The error message

When I run me tests I get this failure message:

Too few invocations for:

1 * list.tail()   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * list.toArray()
1 * list.iterator()
3 * list.size()
1 * list.isEmpty()


    at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
    at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
    at com.fgi.extensions.ListExtensionsTests.add element in setup(ListExtensionsTests.groovy:65)

But from the stdout output (below) I can see that tail() should have been called.

The standard out from the tests

list [a, b] -- 2
Before tailIfAny()
tailIfAny() list.size(): 2
r: [b]                      // This tells me that tail() is being called.
After tailIfAny()

Can anyone shed some light on why Spock thinks that tail() isn't being called? or is there something going on here that I'm missing?

Thanks.

解决方案

Technically, tail() isn't being called on the list object (ArrayList doesn't declare such a method). Therefore, the proxy-based Spy() doesn't get a chance to intercept the call. Try with GroovySpy().

这篇关于Spock spy注册Groovy方法的调用太少(没有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆