Class: Warren::Handler::Test
Overview
Class Warren::Test provides provides a dummy RabbitMQ connection pool for use during testing.
Set up a test warren
By default, the test warren is disabled during testing to avoid storing messages unnecessarily. Instead you must explicitly enable it when you wish to test message receipt.
If using rspec it is suggested that you add the following to your spec_helper.rb
config.around(:each, warren: true) do |ex|
Warren.handler.enable!
ex.run
Warren.handler.disable!
end
Making assertions
It is possible to query the test warren about the messages it has seen. In particular the following methods are useful:
Method: Warren::Handler::Test#messages
- Defined in:
- lib/warren/handler/test.rb
#messages ⇒ Array<#routing_key#payload>
Returns an array of all message received by the warren since it was enabled
|
|
Method: Warren::Handler::Test#last_message
- Defined in:
- lib/warren/handler/test.rb
#last_message ⇒ #routing_key#payload
Returns the last message received by the warren
|
|
Method: Warren::Handler::Test#message_count
- Defined in:
- lib/warren/handler/test.rb
#message_count ⇒ Integer
Returns the total number message received by the warren since it was enabled
|
|
Method: Warren::Handler::Test#messages_matching
- Defined in:
- lib/warren/handler/test.rb
#messages_matching(routing_key) ⇒ Integer
Returns the total number message received by the warren matching the given routing_key since it was enabled
|
|
Example
describe QcResult, warren: true do
let(:warren) { Warren.handler }
setup { warren. }
let(:resource) { build :qc_result }
let(:routing_key) { 'test.message.qc_result.' }
it 'broadcasts the resource' do
resource.save!
expect(warren.(routing_key)).to eq(1)
end
end
Defined Under Namespace
Classes: Channel
Constant Summary collapse
- DISABLED_WARNING =
Warning displayed if the user attempts to make assertions against the handler without having enabled it.
<<~DISABLED_WARREN Test made against a disabled warren. Warren::Handler::Test must be explicitly enabled to track messages, it is a good idea to disable it again after testing the relevant behaviour. This ensures we track messages on a per-test basis, and avoids unnecessary message storage. If using rspec it is suggested that you add the following to your spec_helper.rb config.around(:each, warren: true) do |ex| Warren.handler.enable! ex.run Warren.handler.disable! end You can then tag tests with warren: true to enable warren testing. DISABLED_WARREN
Instance Method Summary collapse
-
#<<(message) ⇒ Object
Disable message logging if not required.
-
#clear_messages ⇒ Array
Clear any logged messaged.
-
#disable! ⇒ Object
Clean up after ourselves to avoid memory leaks.
-
#enable! ⇒ Object
Enable the warren.
-
#initialize(*_args) ⇒ Test
constructor
Creates a test warren with no messages.
-
#last_message ⇒ #routing_key#payload
Returns the last message received by the warren.
-
#message_count ⇒ Integer
Returns the total number message received by the warren since it was enabled.
-
#messages ⇒ Array<#routing_key#payload>
Returns an array of all message received by the warren since it was enabled.
-
#messages_matching(routing_key) ⇒ Integer
Returns the total number message received by the warren matching the given routing_key since it was enabled.
- #new_channel ⇒ Warren::Test::Channel
- #with_channel {|new_channel| ... } ⇒ void
Methods inherited from Base
Constructor Details
#initialize(*_args) ⇒ Test
Creates a test warren with no messages. Test warrens are shared across all threads.
98 99 100 101 102 103 |
# File 'lib/warren/handler/test.rb', line 98 def initialize(*_args) super() @messages = [] @exchanges = [] @enabled = false end |
Instance Method Details
#<<(message) ⇒ Object
Disable message logging if not required
187 188 189 |
# File 'lib/warren/handler/test.rb', line 187 def <<() @messages << if @enabled end |
#clear_messages ⇒ Array
Clear any logged messaged
131 132 133 134 |
# File 'lib/warren/handler/test.rb', line 131 def @messages = [] @exchanges = [] end |
#disable! ⇒ Object
Clean up after ourselves to avoid memory leaks
173 174 175 176 |
# File 'lib/warren/handler/test.rb', line 173 def disable! @enabled = false end |
#enable! ⇒ Object
Enable the warren
167 168 169 170 |
# File 'lib/warren/handler/test.rb', line 167 def enable! @enabled = true end |
#last_message ⇒ #routing_key#payload
Returns the last message received by the warren
141 142 143 |
# File 'lib/warren/handler/test.rb', line 141 def .last end |
#message_count ⇒ Integer
Returns the total number message received by the warren since it was enabled
150 151 152 |
# File 'lib/warren/handler/test.rb', line 150 def .length end |
#messages ⇒ Array<#routing_key#payload>
Returns an array of all message received by the warren since it was enabled
181 182 183 184 |
# File 'lib/warren/handler/test.rb', line 181 def raise_if_not_tracking @messages end |
#messages_matching(routing_key) ⇒ Integer
Returns the total number message received by the warren matching the given routing_key since it was enabled
162 163 164 |
# File 'lib/warren/handler/test.rb', line 162 def (routing_key) .count { || .routing_key == routing_key } end |
#new_channel ⇒ Warren::Test::Channel
Returns a new channel, which proxies all message back to #messages on the Warren::Handler::Test
122 123 124 |
# File 'lib/warren/handler/test.rb', line 122 def new_channel Channel.new(@logger, routing_key_template: @routing_key_template) end |
#with_channel {|new_channel| ... } ⇒ void
This method returns an undefined value.
Yields a new channel, which proxies all message back to #messages on the Warren::Handler::Test
112 113 114 |
# File 'lib/warren/handler/test.rb', line 112 def with_channel yield new_channel end |