Spock框架:间谍问题 [英] Spock Framework: problems with spying

查看:155
本文介绍了Spock框架:间谍问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spock中使用Spy存在一个问题,它要么不能正常工作,要么我的理解错误,所以我试图澄清这一点。
考虑这个代码(Java):

pre code public class CallingClass {

public String functionOne (){
//做东西
返回one;


public String functionTwo(){
String one = functionOne();
返回一些字符串+一个;






$ b现在我想测试 functionTwo 调用 functionOne 以及定义从 functionOne 返回的值(想象例如,如果 functionOne 真的很复杂,我不想在我的测试中执行它只是想将它存根并将其设置为返回某个值)。为此,我在Groovy(使用Spock)中编写了以下测试:

  class CallingClassTest extends Specification {

)defcheck functionTwo calls functionOne(){
def c = Spy(CallingClass)
c.functionOne()>> 嘲讽函数返回

当:
def s = c.functionTwo()

then:
1 * c.functionOne()
s ==some string mocked function return
}
}

为了做这样的事情,Spock要求我有 cglib 库,所以我的构建文件(在Gradle中)看起来像这样:

  apply plugin:'groovy'

repositories {
mavenCentral()
}

dependencies {
compile'org.codehaus.groovy:groovy:latest.release'

testCompile'org.spockframework:spock-core:latest.release'
testCompile'junit:junit:latest.release'
testCompile'c​​glib:cglib-nodep:latest.release'

}

现在当我运行测试时,我预计 functionOne 根本不会被调用,而是我的stubbed版本被使用。相反,我得到这个:

 不满足条件:

s ==some string mocked function return
| |
|假
| 19种差异(44%相似性)
|一些字符串( - )o(-------)n(-------)e(----)
|一些字符串(m)o(cked fu)n(ction r)e(转)
一些字符串一个

条件不满足:

s ==some字符串模拟函数返回
| |
|假
| 19种差异(44%相似性)
|一些字符串( - )o(-------)n(-------)e(----)
|一些字符串(m)o(cked fu)n(ction r)e(转)
一些字符串一个

在CallingClassTest.check那个函数调用functionOne(CallingClassTest.groovy:17)

更重要的是,如果我调试它并在 functionOne 中设置断点, code>它会被调用:($ / b>

我缺少什么?不应该使用存根 functionOne 提供并返回字符串mocked function return

解决方案

<它应该是:

$ $ p $ code $ @Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang。*

class CallingClassTest extends Specification {

def检查functionTwo调用functionOne(){
def c = Spy(CallingClass)

当:
def s = c.functionTwo()

then:
1 * c.functionOne()>>mocked functi返回
s ==一些字符串模拟函数返回
}
}

public class CallingClass {

public String functionOne ){
one
}

public String functionTwo(){
String one = functionOne()
some string $ one


$ / code>

当你们间谍/模拟并验证这是如何返回值应该被指定。


I have an issue with using Spy in Spock, it either doesn't work as it should or my understanding is wrong so I'm trying to clarify this. Consider this code (Java):

public class CallingClass {

    public String functionOne() {
        //does stuff
        return "one";
    }

    public String functionTwo() {
        String one = functionOne();
        return "some string " + one;
    }
}

Now I want to test the fact that functionTwo calls functionOne as well as define the returned value from functionOne (imagine for instance if functionOne is really complicated and I don't want to execute it in my test just want to stub it and set it to return a certain value). For this i write the following test in Groovy (using Spock):

class CallingClassTest extends Specification {

    def "check that functionTwo calls functionOne"() {
        def c = Spy(CallingClass)
        c.functionOne() >> "mocked function return"

        when:
        def s = c.functionTwo()

        then:
        1 * c.functionOne()
        s == "some string mocked function return"
    }
}

In order to do things like this, Spock is asking me to have the cglib library, so my build file (in Gradle) looks something like this:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy:latest.release'

    testCompile 'org.spockframework:spock-core:latest.release'
    testCompile 'junit:junit:latest.release'
    testCompile 'cglib:cglib-nodep:latest.release'

}

Now when I run the test, I expect the functionOne not to be called at all, and instead my stubbed out version to be used. Instead, I get this:

Condition not satisfied:

s == "some string mocked function return"
| |
| false
| 19 differences (44% similarity)
| some string (-)o(-------)n(-------)e(----)
| some string (m)o(cked fu)n(ction r)e(turn)
some string one

Condition not satisfied:

s == "some string mocked function return"
| |
| false
| 19 differences (44% similarity)
| some string (-)o(-------)n(-------)e(----)
| some string (m)o(cked fu)n(ction r)e(turn)
some string one

    at CallingClassTest.check that functionTwo calls functionOne(CallingClassTest.groovy:17)

Even more so, if I debug this and set a breakpoint in functionOne it gets called :(

What am I missing? Shouldn't my test use the stubbed functionOne provided and just return the string "mocked function return"?

解决方案

It should be:

@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class CallingClassTest extends Specification {

    def "check that functionTwo calls functionOne"() {
        def c = Spy(CallingClass)

        when:
        def s = c.functionTwo()

        then:
        1 * c.functionOne() >> "mocked function return"
        s == "some string mocked function return"
    }
}

public class CallingClass {

    public String functionOne() {
        "one"
    }

    public String functionTwo() {
        String one = functionOne()        
        "some string $one"
    }
}

When you both spy/mock and verify this is how return value should be specified.

这篇关于Spock框架:间谍问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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