Skip to content

Fix issue 5 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jsonapi_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'jsonapi_spec_helpers/helpers'
require 'jsonapi_spec_helpers/payload'
require 'jsonapi_spec_helpers/payload_sanitizer'
require 'jsonapi_spec_helpers/errors'

module JsonapiSpecHelpers
def self.included(klass)
Expand Down
16 changes: 16 additions & 0 deletions lib/jsonapi_spec_helpers/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module JsonapiSpecHelpers
module Errors
class Base < StandardError; end
class IncludedOutOfBounds < Base
def initialize(type, index, array)
@type = type; @index = index; @array = array
end

def message
"You attempted to get an item at index #{@index} of the type '#{@type}' " \
"from the included property of your JSON payload. But it contained " \
"#{@array.length} '#{@type}'"
end
end
end
end
6 changes: 5 additions & 1 deletion lib/jsonapi_spec_helpers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def json_includes(type, *indices)
indices = (0...included.length).to_a if indices.empty?
includes = []
indices.each do |index|
includes << json_item(from: included.at(index))
single_included = included.at(index)
if single_included.nil?
raise Errors::IncludedOutOfBounds.new(type, index, included)
end
includes << json_item(from: single_included)
end
includes
end
Expand Down
4 changes: 4 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@
it 'is only includes of a given type at indices' do
expect(json_includes('comments', 1).length).to eq(1)
end

it 'throws when asking for an index beyond the length of the includes' do
expect{ json_includes('comments', 99) }.to raise_error JsonapiSpecHelpers::Errors::IncludedOutOfBounds
end
end
end

Expand Down