jsonapi-rspec icon indicating copy to clipboard operation
jsonapi-rspec copied to clipboard

Matchers to handle data arrays

Open BrennickL opened this issue 4 years ago • 4 comments

Expected Behavior

Should be able to handle Arrays.

Actual Behavior

Does not handle arrays.

Steps to Reproduce the Problem

  1. Create a JSON Serialized object that has the 'data' attribute as an array.
  2. Deserialize the JSON object
  3. Pass the 'data' attribute to the matchers, either as a single node or as an array of nodes.

Specifications

  • Version:
  • jsonapi-rspec (0.0.10) Summary: RSpec matchers for JSON API. Homepage: https://github.com/jsonapi-rb/jsonapi-rspec Path: /Users/brennick.langston/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/jsonapi-rspec-0.0.10
  • Ruby version: ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-darwin19]

BrennickL avatar Nov 12 '20 16:11 BrennickL

@BrennickL it would be great to leave a more concrete example. I'm not sure I understand fully what do you mean by data arrays.

Btw, have you seen this section? https://github.com/jsonapi-rb/jsonapi-rspec#advanced-examples

stas avatar Nov 12 '20 19:11 stas

@stas I think I understand what the OPs problem is because I'm facing a similar one. According to the docs, this should work:

expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])

However what you get if you pass in a structure to test like

{
  "data"=> {
    "id" => ..., "type" => "...",
    "attribuites" => {...},
    "relationships" => {
      "posts": [
        { "data" => { "id" => "1", "type" => "posts" } },
        { "data" => { "id" => "2", "type" => "posts" } },
      ]
    }
  }

is

Failure/Error: expect(json["data"]).to have_relationship(:posts).with_data( [ { "id" => ...}, { "id" => "..." } ] )

     TypeError:
       no implicit conversion of String into Integer

because https://github.com/jsonapi-rb/jsonapi-rspec/blob/master/lib/jsonapi/rspec/relationships.rb#L9 can't handle arrays. It actually expects relationships[:posts] to be an object, but here it's an array.

kassi avatar Feb 18 '21 20:02 kassi

@kassi would you be kind to review this test and help me understand how's that different from your example: https://github.com/jsonapi-rb/jsonapi-rspec/blob/master/spec/jsonapi/relationships_spec.rb#L35-L42

stas avatar Feb 18 '21 21:02 stas

@kassi Your input is ill-formed. Your posts should be an object containing a data member that is an array, rather than being an array containing objects with a data member:

{ "posts": 
  { "data": [
    { "id" => "1", "type" => "posts" },
    { "id" => "2", "type" => "posts" }]
  }
}

Cf. https://jsonapi.org/format/#document-resource-object-linkage

beauby avatar Feb 19 '21 16:02 beauby