【问题标题】:Mocking a method based on the parameter in Spock根据Spock中的参数模拟方法
【发布时间】:2017-01-23 23:31:29
【问题描述】:

我试图弄清楚嘲笑,但由于某种原因,它一直返回正确答案(赢)而不是我希望它返回的答案(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

这是另一个例子。我想我只是在嘲笑。但这是我要测试的方法

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
    }
}

【问题讨论】:

    标签: groovy spock


    【解决方案1】:

    将模拟语句向下移动到“then:”块中。只需将它们置于断言之上。 “和:”是什么让你绊倒。模拟标注属于“设置/给定”块或“然后”块。

    像这样:

    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'
    }
    

    【讨论】:

    • 我试过了,但没有用。当我运行您的代码时,它错误地说 os 不是该类的属性。我将它切换到我刚刚在上面发布的代码和相同的交易,返回 win 而不是 mac
    • @scphantm,您的测试中有多个问题 - 您想测试静态方法,这段代码是用 java 还是 groovy 编写的?
    • OsImpl 很时髦。 OsImpl 扩展的对象是java,import org.apache.tools.ant.taskdefs.condition.Os
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多