Sha256: 6d5965b9ca93d0c4ae861191fdb471d2d26e7a533c5485cb183d88f1fcfbfc09

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require 'json'
require 'jsonapi_spec_helpers/version'
require 'jsonapi_spec_helpers/matchers'
require 'jsonapi_spec_helpers/helpers'
require 'jsonapi_spec_helpers/payload'
require 'jsonapi_spec_helpers/payload_sanitizer'

module JsonapiSpecHelpers
  def self.included(klass)
    klass.send(:include, Helpers)
    if defined?(Rails)
      Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
    end
  end

  def assert_payload(name, record, json, &blk)
    unless payload = JsonapiSpecHelpers::Payload.registry[name]
      raise "No payloads registered for '#{name}'"
    end

    if blk
      payload = payload.fork
      payload.instance_eval(&blk)
    end

    aggregate_failures "payload has correct key/values" do
      payload.keys.each_pair do |attribute, options|
        prc = options[:proc]
        if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
          unless options[:allow_nil]
            expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))

            if options[:type]
              expect(json[attribute.to_s]).to match_type(attribute, options[:type])
            end
          end
        end
      end

      payload.no_keys.each do |no_key|
        expect(json).to_not have_payload_key(no_key, {})
      end

      unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
      unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
      unexpected_keys.each do |key|
        expect(key).to be_not_in_payload
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsonapi_spec_helpers-0.4.2 lib/jsonapi_spec_helpers.rb
jsonapi_spec_helpers-0.4.1 lib/jsonapi_spec_helpers.rb
jsonapi_spec_helpers-0.4.0 lib/jsonapi_spec_helpers.rb