Sha256: fe4c274dc6294dfd861bb3a3b98157c858d5e574ca8837f0ef0f483dd6bc7deb

Contents?: true

Size: 1.9 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
lita-3.0.3 lib/lita/rspec/matchers/route_matcher.rb
lita-3.0.2 lib/lita/rspec/matchers/route_matcher.rb
lita-3.0.1 lib/lita/rspec/matchers/route_matcher.rb
lita-3.0.0 lib/lita/rspec/matchers/route_matcher.rb