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

Expected ? to be array got Hash from JSON response #33

Closed
fmorales opened this issue Jan 7, 2015 · 5 comments
Closed

Expected ? to be array got Hash from JSON response #33

fmorales opened this issue Jan 7, 2015 · 5 comments

Comments

@fmorales
Copy link

fmorales commented Jan 7, 2015

When I hit this uri
http://voteapi-cloud-test.telescope.tv/moderation/getMessages?topic_id=1000336

and use this expectation
expect_json_keys('?', [:position])

I get this error:

  1. Test Endpoint - has the correct keys
    Failure/Error: expect_json_keys('?', [:position])
    Expected ? to be array got Hash from JSON response

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/path_matcher.rb:99:in `ensure_array'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/path_matcher.rb:11:in`block in get_by_path'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/path_matcher.rb:9:in `each'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/path_matcher.rb:9:in`each_with_index'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/path_matcher.rb:9:in `get_by_path'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/request_expectations.rb:87:in`call_with_path'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/request_expectations.rb:23:in `expect_json_keys'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/request_expectations.rb:62:in`call'

    /Users/francesmorales/.rvm/gems/ruby-2.1.2/gems/airborne-0.1.10/lib/airborne/request_expectations.rb:62:in `block (2 levels) in module:RequestExpectations'

    ./spec/sandbox/toys_rspec.rb:22:in`block (2 levels) in <top (required)>'

I get the same error for *

Am I still running into a bug, or am I doing something wrong?

@sethpollack
Copy link
Collaborator

Yes, the ? and * are for checking against elements in an array. Your api is returning a hash. What are you trying to test for?

@fmorales
Copy link
Author

fmorales commented Jan 7, 2015

I am trying to check that all the nodes returned contain :position, instead of just 0. How do I do that?

@sethpollack
Copy link
Collaborator

Offhand, you can loop through the keys on json_body and call expect_json_keys for each.

Something like this:

json_body.keys.each do |el|
  expect_json_keys(el.to_s, [:position])
end

@fmorales I just updated the example the last one had a bug in it.

@brooklynDev
Copy link
Owner

An alternative approach to what @sethpollack posted, is to use a little bit of hackery:

    @json_body = json_body.values
    expect_json_keys('?', [:position])

This relies on some implementation details of airborne but will work as well. The main issue is that you're treating an object like that of an array. If we see more use cases for this in the future, we can add support to airborne for this.

@fmorales
Copy link
Author

fmorales commented Jan 9, 2015

Alex,
Thank you. That’s hacky, but it does in fact do what I need it to do. Thanks for much for pointing out how to do that.
-Frances

On Jan 9, 2015, at 7:56 AM, Alex Friedman notifications@github.com wrote:

@json_body = json_body.values
expect_json_keys('?', [:position])

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

3 participants