Sha256: cc9344628bf4efbdf2654efa1fb5e75fd8d4c94fe8604033b4e3c78e6eacc510

Contents?: true

Size: 1.93 KB

Versions: 38

Compression:

Stored size: 1.93 KB

Contents

module Shoulda # :nodoc:
  module ActionController # :nodoc:
    module Matchers

      # Ensures that a session key was set to the expected value.
      #
      # Example:
      #
      #   it { should set_session(:message) }
      #   it { should set_session(:user_id).to(@user.id) }
      #   it { should_not set_session(:user_id) }
      def set_session(key)
        SetSessionMatcher.new(key)
      end

      class SetSessionMatcher # :nodoc:

        def initialize(key)
          @key = key.to_s
        end

        def to(value)
          @value = value
          self
        end

        def matches?(controller)
          @controller = controller
          (assigned_value? && assigned_correct_value?) || cleared_value?
        end

        def failure_message
          "Expected #{expectation}, but #{result}"
        end

        def negative_failure_message
          "Didn't expect #{expectation}, but #{result}"
        end

        def description
          description = "set session variable #{@key.inspect}"
          description << " to #{@value.inspect}" if defined?(@value)
          description
        end

        private

        def assigned_value?
          !assigned_value.nil?
        end

        def cleared_value?
          defined?(@value) && @value.nil? && assigned_value.nil?
        end

        def assigned_correct_value?
          return true if @value.nil?
          assigned_value == @value
        end

        def assigned_value
          session[@key]
        end

        def session
          @controller.response.session.data
        end

        def expectation
          expectation = "session variable #{@key} to be set"
          expectation << " to #{@value.inspect}" if @value
          expectation
        end

        def result
          if session.empty?
            "no session variables were set"
          else
            "the session was #{session.inspect}"
          end
        end

      end

    end
  end
end

Version data entries

38 entries across 38 versions & 10 rubygems

Version Path
auser-poolparty-1.3.0 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.1 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.10 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.11 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.12 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.13 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.14 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.15 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.16 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.17 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.2 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.3 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.4 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.5 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.6 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.7 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
auser-poolparty-1.3.8 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
fairchild-poolparty-1.3.17 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
fairchild-poolparty-1.3.5 vendor/gems/shoulda/lib/shoulda/action_controller/matchers/set_session_matcher.rb
iGEL-shoulda-2.10.2 lib/shoulda/action_controller/matchers/set_session_matcher.rb