Class: Polyn::Testing::MockNats

Inherits:
Object
  • Object
show all
Defined in:
lib/polyn/testing/mock_nats.rb

Overview

Mock Nats connection for applications to use in testing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nats) ⇒ MockNats

Returns a new instance of MockNats.



14
15
16
17
18
19
# File 'lib/polyn/testing/mock_nats.rb', line 14

def initialize(nats)
  @nats        = nats
  @messages    = []
  @subscribers = []
  @consumers   = []
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



21
22
23
# File 'lib/polyn/testing/mock_nats.rb', line 21

def messages
  @messages
end

#natsObject (readonly)

Returns the value of attribute nats.



21
22
23
# File 'lib/polyn/testing/mock_nats.rb', line 21

def nats
  @nats
end

Instance Method Details

#add_consumer(consumer) ⇒ Object



39
40
41
# File 'lib/polyn/testing/mock_nats.rb', line 39

def add_consumer(consumer)
  @consumers << consumer
end

#jetstreamObject



35
36
37
# File 'lib/polyn/testing/mock_nats.rb', line 35

def jetstream
  @jetstream ||= MockJetStream.new(self)
end

#publish(subject, data, reply_to = nil, **opts) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/polyn/testing/mock_nats.rb', line 23

def publish(subject, data, reply_to = nil, **opts)
  msg = Polyn::Testing::MockMsg.new(subject: subject, data: data, reply: reply_to,
    header: opts[:header])
  send_to_subscribers(msg)
  @messages << msg
  update_consumers
end

#subscribe(subject, _opts = {}, &callback) ⇒ Object



31
32
33
# File 'lib/polyn/testing/mock_nats.rb', line 31

def subscribe(subject, _opts = {}, &callback)
  @subscribers << { subject: subject, callback: callback }
end