Sha256: a9f385d807f7f9879bcc59679ffb618c41669090d986be5b634ab08ac7987e0c

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

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).is(:required)
      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

8 entries across 8 versions & 1 rubygems

Version Path
fortnox-api-0.8.0 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.7.2 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.7.1 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.7.0 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.6.3 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.6.2 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.6.1 spec/fortnox/api/types/model_spec.rb
fortnox-api-0.6.0 spec/fortnox/api/types/model_spec.rb