Sha256: 56fc24f7ceee7fd5abbd77016244af7c31b0a3bfa80a527512d5667e11a269bc

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

class RecordedEvent
  attr_reader :name, :data

  def initialize(name, data)
    @name = name.to_s
    @data = data
  end

  def inspect
    "<Event:#{name} #{inspect_data}>"
  end

  def ==(other)
    name == other.name && data == other.data
  end

  private

  def inspect_data
    data.inject([]) { |result, (key, value)|
      result << "#{key.inspect} => #{value.inspect.slice(0, 20)}"
    }.join(", ")
  end
end

class RecordingObserver
  def self.instance
    @@instance ||= self.new
  end

  attr_accessor :events

  def initialize
    @events = []
  end

  def respond_to?(message)
    true
  end

  def method_missing(name, data)
    @events << RecordedEvent.new(name, data)
  end
end

RSpec::Matchers.define :notify_observers do |event_name, data|
  match do |ignored_subject|
    @event = RecordedEvent.new(event_name, data)
    recorder.events.should include(@event)
  end

  failure_message do
    "Expected event:\n#{@event.inspect}\n\nGot events:\n#{recorder.events.map(&:inspect).join("\n")}"
  end

  def recorder
    RecordingObserver.instance
  end
end

RSpec::Matchers.define :notify_observers_once do |event_name, data|
  match do |ignored_subject|
    @event = RecordedEvent.new(event_name, data)
    recorder.events.select{|ev| ev == @event }.size.should == 1
  end

  failure_message do
    times_notified = recorder.events.select{|ev| ev == @event }.size
    "Expected event 1 time:\n#{@event.inspect}\n\nGot event #{times_notified} times"
  end

  def recorder
    RecordingObserver.instance
  end
end

RSpec.configure do |config|
  config.before do
    Saucy::Notifications.clear_observers
    Saucy::Notifications.register_observer(RecordingObserver.instance)
    RecordingObserver.instance.events = []
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
saucy-0.10.10 spec/support/notifications.rb
saucy-0.10.9 spec/support/notifications.rb
saucy-0.10.8 spec/support/notifications.rb
saucy-0.10.7 spec/support/notifications.rb