Sha256: 5cd9aecedd8ea8464703ce0f923735e7b887e643b9df4975364cadabb5e78623

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

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
        return true if error.message.include? expected_error_message

        @wrong_error_message = error.message
        false
      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

10 entries across 10 versions & 1 rubygems

Version Path
fortnox-api-0.8.2 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.8.1 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.8.0 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.7.2 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.7.1 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.7.0 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.6.3 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.6.2 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.6.1 spec/support/matchers/type/require_attribute_matcher.rb
fortnox-api-0.6.0 spec/support/matchers/type/require_attribute_matcher.rb