class BitGirder::Event::Logger::Testing::EventAccumulator
A simple event listener (see BitGirder::EventLogger) which accumulates all events into an unbounded list. Test classses should make sure to remove this listener from its associated engine after assertions are complete, either explicitly or via ensure_removed
, lest it continue to amass large amounts of unneeded events.
Public Class Methods
Convenience method to create, register, and return an instance which accumulates events from the given EventLogger::Engine
# File lib/bitgirder/event/logger/testing.rb, line 126 def self.create( *argv ) self.new( *argv ).tap { |acc| acc.engine.add_listener( acc ) } end
# File lib/bitgirder/event/logger/testing.rb, line 130 def self.while_accumulating( *argv ) acc = self.create( *argv ) acc.ensure_removed { yield( acc ) } end
Public Instance Methods
# File lib/bitgirder/event/logger/testing.rb, line 76 def assert_logged( expct = nil, &blk ) if expct if blk raise "Illegal combination of expect val and block" else blk = lambda { |ev| ev == expct } end else raise "Block missing" unless blk end assert events.find( &blk ), "Block did not match any events" end
Executes an arbitrary block, ensuring that this instance is removed from the engine with which it is associated whether or not the block completes normally. This method returns the block’s result or raises its exception.
This instance is itself provided to the block, enabling (in conjunction with EventAccumulator.create()
) test code to easily and reliably wrap an entire test:
def test_login_success EventAccumulator.create( ev_eng ).ensure_removed do |acc| # Do a login and first check that it succeeds as wanted login_res = do_login( "ezra", "somepass" ) assert( login_res.ok? ) # Now also check that login event was generated ev_expct = { :event => :login, :user => "ezra", :result => :ok } acc.assert_logged { |ev| ev == ev_expct } ... # Possibly more test code, cleanup, etc end end
# File lib/bitgirder/event/logger/testing.rb, line 116 def ensure_removed begin yield( self ) ensure @engine.remove_listener( self ) end end
Adds ev
to this instance’s event list.
# File lib/bitgirder/event/logger/testing.rb, line 65 def event_logged( ev ) @mut.synchronize { @events << ev } end
Gets a snapshot of the events accumulated so far.
# File lib/bitgirder/event/logger/testing.rb, line 71 def events @mut.synchronize { Array.new( @events ) } end
Creates a new instance associated with the given engine
# File lib/bitgirder/event/logger/testing.rb, line 57 def impl_initialize @mut = Mutex.new @events = [] end