Skip to content
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

Matchers to handle data arrays #27

Open
BrennickL opened this issue Nov 12, 2020 · 4 comments
Open

Matchers to handle data arrays #27

BrennickL opened this issue Nov 12, 2020 · 4 comments

Comments

@BrennickL
Copy link

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]
@stas
Copy link
Collaborator

stas commented Nov 12, 2020

@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

@kassi
Copy link

kassi commented Feb 18, 2021

@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.

@stas
Copy link
Collaborator

stas commented Feb 18, 2021

@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

@beauby
Copy link
Member

beauby commented Feb 19, 2021

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants