Sha256: 3a95788c20d20aeb1c37e695e3c51fa268f2975a653c2a26e6f6339bc30ea4ee

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

module Remarkable # :nodoc:
  module Controller # :nodoc:
    module Matchers # :nodoc:
      class SetSessionMatcher < Remarkable::Matcher::Base
        include Remarkable::Controller::Helpers
        
        def initialize(key, expected=nil, &block)
          @key      = key
          @expected = expected
          @expected = block if block_given?
        end

        def matches?(subject)
          @subject = subject

          initialize_with_spec!

          assert_matcher do
            has_session_key?
          end
        end

        def description
          expectation
        end

        def failure_message
          @missing
        end

        private

        def has_session_key?
          expected_value = if @expected.is_a?(Proc)
            @spec.instance_eval &@expected
          else
            warn "[DEPRECATION] Strings given to should_set_session won't be evaluated anymore. Give a block or a proc instead."
            @spec.instance_eval(@expected) rescue @expected
          end
          return true if @session[@key] == expected_value

          @missing = "Expected #{expected_value.inspect} but was #{@session[@key]}"
          return false
        end

        def initialize_with_spec!
          # In Rspec 1.1.12 we can actually do:
          #
          #   @session = @subject.session
          #
          @session = @spec.instance_eval { session }
        end
        
        def expectation
          "return the correct value from the session for key #{@key}"
        end
      end

      def set_session(key, expected=nil, &block)
        SetSessionMatcher.new(key, expected, &block)
      end

      def return_from_session(*args, &block)
        warn "[DEPRECATION] should_return_from_session is deprecated. Use should_set_session instead."
        set_session(*args, &block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
carlosbrando-remarkable-2.2.10 lib/remarkable/controller/macros/set_session_matcher.rb
carlosbrando-remarkable-2.2.7 lib/remarkable/controller/macros/set_session_matcher.rb
carlosbrando-remarkable-2.2.8 lib/remarkable/controller/macros/set_session_matcher.rb
carlosbrando-remarkable-2.2.9 lib/remarkable/controller/macros/set_session_matcher.rb