Sha256: b6d2ff9c52eb66543ef5168a88b075160bccf4976ce1f1870e3d0c6d38706fe0

Contents?: true

Size: 1.35 KB

Versions: 34

Compression:

Stored size: 1.35 KB

Contents

module Stream
  module Matchers
    class FireEvent
      def initialize(event_type, opts = {})
        @event_type = event_type
        @opts = opts
        @method = :"fire_#{@event_type}_after_#{@opts[:on]}"
      end

      def matches?(subject)
        @subject = subject

        defines_callback_method? && setups_up_callback?
      end

      def defines_callback_method?
        if @subject.instance_methods.include?(@method.to_s)
          true
        else
          @missing = "#{@subject.name} does not respond to #{@method}"
          false
        end
      end

      def setups_up_callback?
        callback_chain_name = "after_#{@opts[:on]}_callback_chain"
        callback_chain = @subject.send(callback_chain_name)
        if callback_chain.any? {|chain| chain.method == @method }
          true
        else
          @missing = "does setup after #{@opts[:on]} callback for #{@method}"
          false
        end
      end

      def description
        "fire a #{@event_type} event"
      end

      def expectation
        expected = "#{@subject.name} to #{description}"
      end

      def failure_message
        "Expected #{expectation} (#{@missing})"
      end

      def negative_failure_message
        "Did not expect #{expectation}"
      end

    end

    def fire_event(event_type, opts)
      FireEvent.new(event_type, opts)
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
Stream-0.2.3.1 lib/stream/matchers.rb
Stream-0.2.3.0 lib/stream/matchers.rb
Stream-0.2.3 lib/stream/matchers.rb
Stream-0.2.2 lib/stream/matchers.rb
Stream-0.2.1 lib/stream/matchers.rb
Stream-0.2.0 lib/stream/matchers.rb
Stream-0.1.24 lib/stream/matchers.rb
Stream-0.1.23 lib/stream/matchers.rb
Stream-0.1.22 lib/stream/matchers.rb
Stream-0.1.21 lib/stream/matchers.rb
Stream-0.1.20 lib/stream/matchers.rb
Stream-0.1.19 lib/stream/matchers.rb
Stream-0.1.18 lib/stream/matchers.rb
Stream-0.1.16 lib/stream/matchers.rb
Stream-0.1.15 lib/stream/matchers.rb
Stream-0.1.14 lib/stream/matchers.rb
Stream-0.1.13 lib/stream/matchers.rb
Stream-0.1.12 lib/stream/matchers.rb
Stream-0.1.11 lib/stream/matchers.rb
Stream-0.1.10 lib/stream/matchers.rb