Skip to content

Instantly share code, notes, and snippets.

@mario-rezende-ifood
Last active December 11, 2019 16:05
Show Gist options
  • Save mario-rezende-ifood/6458d8fd533bee186a4fe59c12f320c9 to your computer and use it in GitHub Desktop.
Save mario-rezende-ifood/6458d8fd533bee186a4fe59c12f320c9 to your computer and use it in GitHub Desktop.
class Publisher {
List<Subscriber> subscribers = []
void send(String message){
subscribers*.receive(message)
}
}
interface Subscriber {
void receive(String message)
}
class PublisherSpec extends Specification {
Publisher publisher = new Publisher()
Subscriber subscriber1 = Mock()
Subscriber subscriber2 = Mock()
def setup() {
publisher.subscribers << subscriber1 // << operador do Groovy para List.add()
publisher.subscribers << subscriber2
}
def 'should send messages to all subscribers'() {
when:
publisher.send("hello")
then:
1 * subscriber1.receive("hello")
1 * subscriber2.receive("hello")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment