Spock Web Console

subscribe to the feed Subscribe
Untitled (via #spockwebconsole)
tweet this script Tweet

Untitled

Published 2 years ago by Anonymous
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

class Publisher {
    List<Subscriber> subscribers = []
    void send(String message){
        subscribers.each {it.receive message}
    }
}
interface Subscriber {
    void receive(String message)
} 
class MyFirstSpec extends Specification {
  def "let's try this!"() {
    Publisher publisher = new Publisher()
    Subscriber subscriber = Mock()
    Subscriber subscriber2 = Mock()    
    Subscriber subscriber3 = Mock()   
    
    publisher.subscribers << subscriber 
    publisher.subscribers << subscriber2
    publisher.subscribers << subscriber3

    when:
    publisher.send("ala")

    then:
    1 * subscriber.receive("ala")
    0 * _.receive("ala")
  }
}