Skip to content

Fix for issue #8 #14

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 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 Expand Up @@ -52,5 +53,13 @@ def assert_payload(name, record, json, &blk)
expect(key).to be_not_in_payload
end
end

aggregate_failures "payload contains an id and type" do
unless record.id.nil?
expect(json['id']).to eq(record.id.to_s)
end

expect(json['jsonapi_type']).to_not be_nil
end
end
end
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
54 changes: 46 additions & 8 deletions spec/assert_payload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
end
end

context 'when payload is valid' do
let(:post_record) do
double \
id: 1,
title: 'post title',
description: 'post description',
views: 100
end
let(:post_record) do
double \
id: 1,
title: 'post title',
description: 'post description',
views: 100
end

context 'when payload is valid' do
it 'passes assertion' do
assert_payload(:post, post_record, json_item)
end
Expand Down Expand Up @@ -235,5 +235,43 @@
end
end
end

context 'when payload contains a "relationship-only" item' do
before do
JsonapiSpecHelpers::Payload.register(:no_attribute_item) do
end
end

let(:no_attribute_record) do
double \
id: 1,
posts: post_record
end

let(:json) do
{
'data' => {
'type' => 'no_attributes_items',
'id' => '1',
'relationships' => {
'posts' => {
'data' => [
{'type' => 'posts', 'id' => '1'}
]
}
}
}
}
end

it 'still properly throws when no item is present' do
empty_json_item = {'id' => nil, 'jsonapi_type' => nil}
expect {
assert_payload(:no_attribute_item, no_attribute_record, empty_json_item)
}.to raise_error(
RSpec::Expectations::ExpectationNotMetError
)
end
end
end
end