我试图弄清楚嘲笑,并且由于某种原因,它会一直返回正确的答案(胜利)而不是我希望它返回的那个(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
}
仍然返回窗口,而不是mac
这是另一个例子。我想我只是在嘲笑。但这是我想测试的方法
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()对象上得到一个null。现在,除了模拟它之外,我如何检索方法放入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
}
}
答案 0 :(得分:0)
将模拟语句下移到你的&#34;然后:&#34;块。只要将它们置于断言之上。 &#34;和:&#34;是什么让你绊倒。模拟标注属于&#34;设置/给定&#34;阻止或&#34;然后&#34;块。
像这样:
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'
}