Sha256: 7273f3ee9eda0139d2062590b27fc1b93681cdf38e0b2f7b0166ac58721fa826

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require "warp/matcher"

module Warp
  module ControllerMatchers
    class SetFlashMatcher < Warp::Matcher
      attr_reader :flash_key, :expected_flash_value
      attr_reader :controller, :failure_message, :failure_message_when_negated, :description

      def initialize(flash_key, controller)
        @flash_key = flash_key
        @controller = controller
      end

      def to(expected_flash_value)
        @expected_flash_value = expected_flash_value
        self
      end

      def matches?(actual)
        @controller = actual if actual.is_a?(ActionController::Metal)

        return check_expected_value if expected_flash_value
        return check_assigned
      end

      private

      def check_expected_value
        @description = "set flash[:#{flash_key}] to #{expected_flash_value.inspect}"
        @failure_message = "expected flash[:#{flash_key}] to be set to #{expected_flash_value.inspect}"
        @failure_message_when_negated = "expected flash[:#{flash_key}] to not be set to #{expected_flash_value.inspect}"

        values_match?(expected_flash_value, flash_value)
      end

      def check_assigned
        @description = "set flash[:#{flash_key}]"
        @failure_message = "expected flash[:#{flash_key}] to be set"
        @failure_message_when_negated = "expected flash[:#{flash_key}] to not be set"

        values_match?(false, flash_value.nil?)
      end

      def flash_value
        flash[flash_key]
      end

      def flash
        controller.flash
      end
    end

    def set_flash(flash_key)
      SetFlashMatcher.new(flash_key, controller)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
warp-1.5.0 lib/warp/controller_matchers/set_flash_matcher.rb
warp-1.4.0 lib/warp/controller_matchers/set_flash_matcher.rb
warp-1.3.3 lib/warp/controller_matchers/set_flash_matcher.rb
warp-1.3.2 lib/warp/controller_matchers/set_flash_matcher.rb
warp-1.3.1 lib/warp/controller_matchers/set_flash_matcher.rb
warp-1.3.0 lib/warp/controller_matchers/set_flash_matcher.rb