Sha256: 5fc5ee139e39625cf008b90e25b661690765d52aff31f36fdf0aade08572f806

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require_relative '../test_helper'

class ActiveModelErrorTest < Minitest::Spec
  class Order
    include ActiveModel::Model
    attr_accessor :customer_name, :description
    validates :customer_name, presence: true
    validates :description, presence: true
  end

  let(:invalid_order) { Order.new.tap(&:valid?) }

  describe SimpleJsonapi::Errors::ActiveModelError do
    describe "#initialize" do
      let(:error) { SimpleJsonapi::Errors::ActiveModelError.new("the_attribute", "the_message", "the_pointer") }

      it "takes an attribute, a message, and a pointer" do
        assert_equal "the_attribute", error.attribute
        assert_equal "the_message", error.message
        assert_equal "the_pointer", error.pointer
      end
    end

    describe ".from_errors" do
      let(:pointer_mapping) do
        {
          customer_name: "/data/attributes/customer_name",
          description: "/data/attributes/description",
        }
      end

      let(:errors) do
        SimpleJsonapi::Errors::ActiveModelError.from_errors(invalid_order.errors, pointer_mapping)
      end

      it "converts an ActiveModel::Errors object" do
        assert_equal ["customer_name", "description"], errors.map(&:attribute).sort
        assert_equal ["Customer name can't be blank", "Description can't be blank"], errors.map(&:message).sort
      end

      it "incorporates a pointer mapping" do
        assert_equal ["/data/attributes/customer_name", "/data/attributes/description"], errors.map(&:pointer).sort
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_jsonapi_rails-1.3.0 test/errors/active_model_error_test.rb
simple_jsonapi_rails-1.2.0 test/errors/active_model_error_test.rb
simple_jsonapi_rails-1.1.1 test/errors/active_model_error_test.rb
simple_jsonapi_rails-1.1.0 test/errors/active_model_error_test.rb
simple_jsonapi_rails-1.0.0 test/errors/active_model_error_test.rb