Sha256: d4210ec2a6cbfc3e941eeb9aa4ba43a7d30a07c9fc5f76f37f19ae0c2392e7ce

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

module JsonTestHelper
  extend ActiveSupport::Concern

  private

  def assert_json_objects(objects, json_objects, *fields)
    json_objects = [json_objects].flatten

    objects.flatten!
    json_objects.flatten!

    unless json_objects.empty? || json_objects.first.exclude?(:id)
      objects.sort_by!(&:id)
      json_objects.sort_by! { |e| e[:id] }
    end
    assert_equal objects.size, json_objects.size
    objects.zip(json_objects).each do |object, json_object|
      assert_json_object(object, json_object, *fields)
    end
  end

  def assert_json_object(object, json_object, *fields)
    fail(ArgumentError, "fields can't be empty") if fields.blank?
    fields.each do |field|
      expected_value = object.send(field)

      case
      when expected_value.is_a?(ActiveSupport::TimeWithZone)
        assert_equal expected_value.iso8601, json_object[field]
      when expected_value.is_a?(Date)
        assert_equal Oj.load(expected_value.to_json), json_object[field]
      when expected_value.is_a?(Enumerable) && expected_value.all? { |e| e.is_a?(Hash) }
        assert_equal expected_value.map(&:symbolize_keys), json_object[field]
      when expected_value.is_a?(Enumerable)
        assert_equal expected_value.sort, json_object[field].sort
      when expected_value.is_a?(Symbol)
        assert_equal expected_value.to_s, json_object[field]
      else
        assert_equal expected_value, json_object[field]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fogged-0.0.13 test/controllers/concerns/json_test_helper.rb
fogged-0.0.12 test/controllers/concerns/json_test_helper.rb
fogged-0.0.11 test/controllers/concerns/json_test_helper.rb
fogged-0.0.10 test/controllers/concerns/json_test_helper.rb
fogged-0.0.9 test/controllers/concerns/json_test_helper.rb
fogged-0.0.8 test/controllers/concerns/json_test_helper.rb
fogged-0.0.7 test/controllers/concerns/json_test_helper.rb
fogged-0.0.6 test/controllers/concerns/json_test_helper.rb
fogged-0.0.5 test/controllers/concerns/json_test_helper.rb
fogged-0.0.4 test/controllers/concerns/json_test_helper.rb
fogged-0.0.3 test/controllers/concerns/json_test_helper.rb
fogged-0.0.2 test/support/concerns/json_test_helper.rb