Spock Web Console

subscribe to the feed Subscribe
How to partial mock in Spock (via #spockwebconsole)
tweet this script Tweet

How to partial mock in Spock

Published 11 months ago by Dhanaraj0 with tags partial mock
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
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"
  }
}