Sha256: d4c9c7906d312226ad7c35a923f5f8709194ba77aecdfa95d25120e26f854a80
Contents?: true
Size: 1.35 KB
Versions: 2
Compression:
Stored size: 1.35 KB
Contents
# frozen_string_literal: true module Matchers module Type def have_nullable_string(attribute, valid_hash = {}) HaveNullableStringMatcher.new(attribute, valid_hash) end class HaveNullableStringMatcher < AttributeMatcher def initialize(attribute, valid_hash) super(attribute, valid_hash, 'nullable string') end def matches?(klass) super accepts_nil? && accepts_string? && rejects_non_string? end private def accepts_nil? model = @klass.new(@valid_hash.merge(@attribute => nil)) model.send(@attribute).nil? end def accepts_string? valid_string = 'A string' model = @klass.new(@valid_hash.merge(@attribute => valid_string)) model.send(@attribute) == 'A string' end def rejects_non_string? non_string = 10.0 @klass.new(@valid_hash.merge(@attribute => non_string)) rescue Fortnox::API::AttributeError => e expected_message = "#{non_string.inspect} (#{non_string.class}) "\ "has invalid type for #{@attribute.inspect}" return true if e.message.include?(expected_message) fail_message = "Expected error message to include #{expected_message.inspect}, "\ "but was #{e.message.inspect}" raise(fail_message) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
fortnox-api-0.8.2 | spec/support/matchers/type/have_nullable_string_matcher.rb |
fortnox-api-0.8.1 | spec/support/matchers/type/have_nullable_string_matcher.rb |