Groovy 模拟中的spock可重用闭包
如何在spock Mock中重用闭包 我可以按原样键入closure(mock.sth({closure}),但如何创建要重用的命名clsoure或提高代码可读性(mock.sth(hasstuffaspected)) 我有课:Groovy 模拟中的spock可重用闭包,groovy,spock,Groovy,Spock,如何在spock Mock中重用闭包 我可以按原样键入closure(mock.sth({closure}),但如何创建要重用的命名clsoure或提高代码可读性(mock.sth(hasstuffaspected)) 我有课: class Link { String url boolean openInNewWindow } 现在有服务了 interface LinkService { boolean checkStuff(Link link) } 在我的测试中,我想创建如下内
class Link {
String url
boolean openInNewWindow
}
现在有服务了
interface LinkService {
boolean checkStuff(Link link)
}
在我的测试中,我想创建如下内容:
@Shared def linkIsOpenInNewWindow = { Link l -> l.isOpenInNewWindow }
@Shared Closure linkIsHttps = { Link l -> l.url.startsWith("https") }
expect:
1 * linkService.checkStuff(linkIsHttps) >> true
1 * linkService.checkStuff(linkIsOpenInNewWindow) >> false
当我运行这段代码时,它总是:
Too few invocations for:
1 * linkService.checkStuff(linkIsHttps) >> true (0 invocations)
Unmatched invocations (ordered by similarity):
1 * linkService.checkStuff(Link@1234)
有没有办法实现这一点,并在spock中创建和使用命名的可重用闭包
假规格:
@Grab('org.spockframework:spock-core:1.0-groovy-2.4')
@Grab('cglib:cglib:3.1')
@Grab('org.ow2.asm:asm-all:5.0.3')
import spock.lang.Shared
import spock.lang.Specification
class Test extends Specification {
LinkService linkService = Mock(LinkService)
@Shared
Closure openInNewWindow = { it.isOpenInNewWindw()}
@Shared
def httpsLink = { Link l -> l.url.startsWith("https") }
@Shared
def secureLink = new Link(
url: "https://exmaple.com",
openInNewWindw: false
)
@Shared
def externalLink = new Link(
url: "http://exmaple.com",
openInNewWindw: true
)
def "failing closure test"() {
when:
def secureLinkResult = linkService.doStuff(secureLink)
def externalLinkResult = linkService.doStuff(externalLink)
then:
secureLinkResult == 1
externalLinkResult == 2
1 * linkService.doStuff(httpsLink) >> 1
1 * linkService.doStuff(openInNewWindow) >> 2
}
def "passing closure test"() {
when:
def secureLinkResult = linkService.doStuff(secureLink)
def externalLinkResult = linkService.doStuff(externalLink)
then:
secureLinkResult == 1
externalLinkResult == 2
1 * linkService.doStuff({Link l -> l.url.startsWith("https")}) >> 1
1 * linkService.doStuff({Link l -> l.openInNewWindw}) >> 2
}
}
class Link {
String url
boolean openInNewWindw
}
interface LinkService {
int doStuff(Link link)
}
你可以这样写:
def "failing closure test"() {
when:
def secureLinkResult = linkService.doStuff(secureLink)
def externalLinkResult = linkService.doStuff(externalLink)
then:
secureLinkResult == 1
externalLinkResult == 2
1 * linkService.doStuff({ httpsLink(it) }) >> 1
1 * linkService.doStuff({ openInNewWindow(it) }) >> 2
}
可以找到一些解释nope。它不起作用。闭包没有执行。
def httpsLink={Link l->{print(“asd”);return l.url.startsWith(“https”)}
并且没有打印asd文本。看起来闭包没有执行,要使其正常工作,您需要1*linkService.doStuff({httpsLink(It)})
这不完全是我所希望的,但它确实是某种东西;)我不确定为什么打印不起作用,但当我将secureLink定义更改为某个应该打破测试的东西时(等式url:http://exmaple.com
)然后测试失败,但没有打印任何内容。当def secureLink=new Link(url:http://exmaple.com",openInNewWindw:false)(无https)
验证为:1*linkService.doStuff({httpsLink})>>1
然后JUnit4运行程序,测试:1,失败:0,时间:167
除非我用{httpsLink(It)}
验证它,否则它将继续通过。我不知道发生了什么事。我将groovy 2.4.3与Java8一起使用是的,你是对的。很抱歉让您的评论混乱。使用{httpsLink(it)}
作为匹配器正在工作,所以您可以将答案标记为正确答案。