如何在spock测试中的where子句中调用方法或闭包(how to call a method or closure in a where clause in a spock test)

编程入门 行业动态 更新时间:2024-10-24 06:36:27
如何在spock测试中的where子句中调用方法或闭包(how to call a method or closure in a where clause in a spock test)

我正在使用Spock测试atm,我想知道这样的事情是否可能。 我的方法不起作用,我想知道你们中是否有人有类似的意图并找到了办法。

我想调用一个方法或一个闭包,它只能为每个相应的where子句调用,以便设置一些东西。 我不能只是调用所有这些方法,因为它会破坏我的测试。 到目前为止我找到的唯一方法是检查当前状态是什么,并在if语句中相应地调用方法:if(state == SomeStateEnum.FIRST_STATE){somePrivateMethodFromSpec()}但我不知道是否无法完成以更好的方式。 我希望我的意图很明确(抱歉,我不是母语人士)下面是一些示例代码,可能会更好地了解我想要做什么。 先谢谢你。

def 'is this even possible?'() { when: def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | method SomeStateEnum.FIRST_STATE | 'param1' | somePrivateMethodFromSpec() SomeStateEnum.SECOND_STATE | 'param2' | someOtherPrivateMethodFromSpec() } private def somePrivateMethodFromSpec() { someServiceMock.demand.AAA() {} } private def someOtherPrivateMethodFromSpec() { someServiceMock.demand.BBB() {} } def 'or maybe something like this?'() { when: closure.call() def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | closure SomeStateEnum.FIRST_STATE | 'param1' | {println '1'} SomeStateEnum.SECOND_STATE | 'param2' | {println '2'} }

解决方案是:

def 'this will work'() { "$someOtherPrivateMethodFromSpec"() "$somePrivateMethodFromSpec"() def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | method SomeStateEnum.FIRST_STATE | 'param1' | "somePrivateMethodFromSpec" SomeStateEnum.SECOND_STATE | 'param2' | "someOtherPrivateMethodFromSpec" } private def somePrivateMethodFromSpec() { someServiceMock.demand.AAA() {} } private def someOtherPrivateMethodFromSpec() { someServiceMock.demand.BBB() {} }

I'm working with Spock tests atm and I wonder if anything like this is even possbile. My approaches don't work and I wonder if anyone of you had similiar intentions and found a way.

I want to call a method or a closure which must only be called for each respective where-clause in order to setup some things. I can not just call all of these methods as it would ruin my test. The only way I found so far is to check what the current state is and call the method accordingly in an if statement like: if(state==SomeStateEnum.FIRST_STATE){somePrivateMethodFromSpec()} but I wonder if it couldn't be done in a better way. I hope my intentions are clear (sorry, I'm no native speaker) Below is some example code which may be a bit better to understand of what I want to do. Thank you in advance.

def 'is this even possible?'() { when: def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | method SomeStateEnum.FIRST_STATE | 'param1' | somePrivateMethodFromSpec() SomeStateEnum.SECOND_STATE | 'param2' | someOtherPrivateMethodFromSpec() } private def somePrivateMethodFromSpec() { someServiceMock.demand.AAA() {} } private def someOtherPrivateMethodFromSpec() { someServiceMock.demand.BBB() {} } def 'or maybe something like this?'() { when: closure.call() def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | closure SomeStateEnum.FIRST_STATE | 'param1' | {println '1'} SomeStateEnum.SECOND_STATE | 'param2' | {println '2'} }

The solution is:

def 'this will work'() { "$someOtherPrivateMethodFromSpec"() "$somePrivateMethodFromSpec"() def resultState = service.someServiceMethod(param) then: resultState == state where: state | param | method SomeStateEnum.FIRST_STATE | 'param1' | "somePrivateMethodFromSpec" SomeStateEnum.SECOND_STATE | 'param2' | "someOtherPrivateMethodFromSpec" } private def somePrivateMethodFromSpec() { someServiceMock.demand.AAA() {} } private def someOtherPrivateMethodFromSpec() { someServiceMock.demand.BBB() {} }

最满意答案

不确定这是否正是您所需要的,但您可以尝试:

class MyFirstSpec extends Specification { def "let's try this!"() { expect: "${method}"() == method where: method << ["method1", "method2"] } private String method1(){ return "method1" } private String method2(){ return "method2" } }

Not sure if this is exactly what you need but you can try it:

class MyFirstSpec extends Specification { def "let's try this!"() { expect: "${method}"() == method where: method << ["method1", "method2"] } private String method1(){ return "method1" } private String method2(){ return "method2" } }

更多推荐

本文发布于:2023-04-29 03:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句中   方法   如何在   测试中   spock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!