Sha256: 055d7c4bfed8f48075956c08b4abded2108df7388fc5c129bb6fb9777d885430

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

require 'spec_helper'
require 'dry-struct'
require 'fortnox/api/types/model'

RSpec.describe Fortnox::API::Types::Model do
  shared_examples_for 'raises error' do |error|
    using_test_classes do
      module Types
        include Dry::Types.module

        Age = Int.constrained(gt: 18).with(required: true)
      end

      class TypesModelUser < Fortnox::API::Types::Model
        attribute :age, Types::Age
      end
    end

    describe "User inheriting from #{ described_class }" do
      subject{ ->{ TypesModelUser.new(args) } }

      it{ is_expected.to raise_error(error) }
    end
  end

  context 'without required keys' do
    include_examples 'raises error', Fortnox::API::MissingAttributeError do
      let(:args){ {} }
    end
  end

  context 'when omitting optional keys' do
    using_test_class do
      module Types
        include Dry::Types.module

        module Nullable
          String = Types::Strict::String.optional
        end
      end

      class User < Fortnox::API::Types::Model
        attribute :optional_string, Types::Nullable::String
      end
    end

    subject{ ->{ User.new() } }

    it{ is_expected.not_to raise_error }

    describe 'optional attribute' do
      subject{ User.new().optional_string }
      it{ is_expected.to be nil }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fortnox-api-0.5.2 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.5.1 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.5.0 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.4.0 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.3.0 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.2.0 spec/fortnox/api/types/model_spec.rb