How to partial mock in Spock
import spock.lang.*
class PokerHand {
def determineRank() {
if (hasPair()) { return "pair" }
if (hasFullHouse()) { return "full house" }
return "foo"
}
def hasPair() { return false }
def hasFullHouse() { return false }
}
class PublisherSpec extends Specification {
def ph = new PokerHand()
def "evaluates rank 'foo' correctly"() {
expect:
ph.determineRank() == "foo"
}
def "evaluates rank in correct order"() {
given:
def ph = new PokerHand()
and: 'partial mock hasPair method'
ph.metaClass.hasPair = {true}
expect:
ph.determineRank() == "pair"
}
}