Sha256: 9a16b0a707e1d51eaa4ea7ee366e295983f78cefed60f8536206083dd8378d7f

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

require 'fortnox/api'

module Matchers
  module Type
    def require_attribute(attribute, valid_hash = {})
      RequireAttributeMatcher.new(attribute, valid_hash)
    end

    class RequireAttributeMatcher
      EXCEPTION = Fortnox::API::MissingAttributeError

      def initialize( attribute, valid_hash )
        @attribute = attribute
        @invalid_hash = valid_hash.dup.tap{ |hash| hash.delete( attribute ) }
      end

      def matches?( klass )
        @klass = klass

        raise_error && includes_error_message
      end

      def failure_message
        return no_exception_failure_message unless raise_error
        return wrong_error_message unless includes_error_message
      end

      def description
        "require attribute #{ @attribute.inspect }"
      end

      private

        def raise_error
          @klass.new(@invalid_hash)

          false # Fail test since expected error is not thrown

        rescue EXCEPTION
          true
        end

        def includes_error_message
          @klass.new(@invalid_hash)
        rescue EXCEPTION => error
          if error.message.include? expected_error_message
            return true
          else
            @wrong_error_message = error.message
            return false
          end
        end

        def expected_error_message
          @attribute.to_s
        end

        def no_exception_failure_message
          "Expected class to raise #{ EXCEPTION } "\
            "when attribute #{ @attribute.inspect } is missing."
        end

        def wrong_error_message
          "Expected exception to equal #{ expected_error_message.inspect }. "\
            "Message was #{ @wrong_error_message.inspect }."
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fortnox-api-0.5.2 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.5.1 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.5.0 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.4.0 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.3.0 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.2.0 spec/support/matchers/type/require_attribute_matcher.rb