Sha256: 4a815c26ea806969a3332d5f0ce5f007fe74c5dffed0ff68cf8b91994cf03472

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

module Lita
  module RSpec
    # A namespace to hold all of Lita's RSpec matchers.
    module Matchers
      # Used to complete a chat routing test chain.
      class RouteMatcher
        attr_accessor :context, :expectation, :message_body
        attr_reader :expected_route

        def initialize(context, message_body, expectation: true)
          self.context = context
          self.message_body = message_body
          self.expectation = expectation
          set_description
        end

        # Sets an expectation that a route will or will not be triggered, then
        # sends the message originally provided.
        # @param route [Symbol] The name of the method that should or should not
        #   be triggered.
        # @return [void]
        def to(route)
          self.expected_route = route

          e = expectation
          b = message_body
          i = i18n_key

          context.instance_eval do
            allow(Authorization).to receive(:user_in_group?).and_return(true)
            called = false
            allow(subject).to receive(route) { called = true }
            send_message(b)
            expect(called).to be(e), I18n.t(i, message: b, route: route)
          end
        end

        private

        def description_prefix
          if expectation
            "routes"
          else
            "doesn't route"
          end
        end

        def expected_route=(route)
          @expected_route = route
          set_description
        end

        def i18n_key
          if expectation
            "lita.rspec.route_failure"
          else
            "lita.rspec.negative_route_failure"
          end
        end

        def set_description
          description = %(#{description_prefix} "#{message_body}")
          description << " to :#{expected_route}" if expected_route
          ::RSpec.current_example.metadata[:description] = description
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lita-3.3.1 lib/lita/rspec/matchers/route_matcher.rb
lita-3.3.0 lib/lita/rspec/matchers/route_matcher.rb
lita-3.2.0 lib/lita/rspec/matchers/route_matcher.rb
lita-3.1.0 lib/lita/rspec/matchers/route_matcher.rb
lita-3.0.4 lib/lita/rspec/matchers/route_matcher.rb