gpt4 book ai didi

groovy - 根据 Spock 中的参数模拟方法

转载 作者:行者123 更新时间:2023-12-05 07:45:17 24 4
gpt4 key购买 nike

我试图找出模拟,出于某种原因它一直返回正确答案 (win) 而不是我希望它返回的答案 (mac)

/**
* This is an empty imple class. Its intention is to allow operating
* system specific information to be mocked for unit testing.
*/
class OsImpl extends Os {

}


/**
* A utility convenience class that helps with pulling a variety of machine information for the build system
*/
class ProjectInfo {
static String operatingSystem(){
operatingSystem(new OsImpl())
}
/**
* Returns a 3 char code on the operating system of the local machine.
* @return String Code ['win', 'mac', 'nix']
*/
static String operatingSystem(OsImpl os){
if (os.isFamily(os.FAMILY_WINDOWS)) {
'win'
} else if (os.isFamily(os.FAMILY_MAC)) {
'mac'
} else if (os.isFamily(os.FAMILY_UNIX)) {
'nix'
} else {
null
}
}
}

class ProjectInfoTest extends Specification {

def "trying this out"(){
when:
OsImpl os = Mock(OsImpl)

and: 'mock a mac'
1 * os.isFamily(os.FAMILY_WINDOWS) >> false
1 * os.isFamily(os.FAMILY_MAC) >> true

then: 'should return a mac os'
ProjectInfo.operatingSystem(os) == 'mac'
}
}

在 easymock 中,我通常会做一个 when(...).return(...) 但我不确定如何用 spock 做同样的事情。

第二次尝试

def "trying this out"(){
setup:
OsImpl os = Mock(OsImpl)
1 * os.isFamily(os.FAMILY_WINDOWS) >> false
1 * os.isFamily(os.FAMILY_MAC) >> true

when:
String shouldBe = 'mac'

then: 'should return a mac os'
ProjectInfo.operatingSystem(os) == shouldBe
}

仍然返回 windows,而不是 mac

这是另一个例子。我想我只是不擅长 mock 。但这是我要测试的方法

def switchArtifactoryOffline(){
def config = new XmlSlurper().parseText( artifactoryClient.system().configuration())
if (config.offlineMode == false){
config.offlineMode = true
artifactoryClient.system().configuration(XmlUtil.serialize( config ))
}
log.lifecycle("Disabled Artifactory Internet Access")
}

和我的测试方法

def "check that it switches artifactory offline"(){
when:
Artifactory arti = Mock(Artifactory)
arti.system().configuration() >> "<client><offlineMode>false</offlineMode></client>"
ArtifactoryWorker worker = new ArtifactoryWorker(arti)

then:
worker.switchArtifactoryOffline()
}

我一直在 system() 对象上得到一个空值。现在,除了模拟它之外,我如何检索该方法放入 artifactoryClient.system().configuration(XmlUtil.serialize( config )) 的值,以便我可以确保它实际上是否更改了值并正确编译了 xml?这对我来说似乎都是微不足道的事情,我不明白为什么我会遇到这样的问题。也许我确实需要切换回 mockito。

好吧,经过更多的折腾之后,我想出了这个,它似乎有效,现在回到静态......

def "check that it switches artifactory offline"(){
given:
Artifactory arti = Mock(Artifactory)
ArtifactorySystem arti_system = Mock(ArtifactorySystem)

arti_system.configuration() >> "<client><offlineMode>false</offlineMode></client>"
arti.system() >> arti_system

ArtifactoryWorker worker = new ArtifactoryWorker(arti)

when:
worker.switchArtifactoryOffline()

then:
1 * arti_system.configuration(_ as String) >> {
def config = new XmlSlurper().parseText(it)
assert config.offlineMode == true
}
}

最佳答案

将模拟语句向下移动到“then:” block 中。只是让它们高于断言。 “和:”是让你绊倒的原因。模拟标注属于“setup/given” block 或“then” block 。

像这样:

def "trying this out"(){
setup:
OsImpl os = Mock(OsImpl)

expect: 'should return a mac os'
1 * os.isFamily(os.FAMILY_WINDOWS) >> false
1 * os.isFamily(os.FAMILY_MAC) >> true

ProjectInfo.operatingSystem(os) == 'mac'
}

关于groovy - 根据 Spock 中的参数模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41817558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com