collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

at "path" should include: step not acting as I would expect #20

Closed Chuckv closed 12 years ago

Chuckv commented 12 years ago

(apologies in advance for the steps, they need to be cleaned up a fair bit)

My scenario looks like this

Scenario Outline:  GET to OR Packages for specific package uuid returns proper data
  Given I create a request body from the following JSON data
  """
  {
    "package":
    {
      "name": "anothertestpackage", 
      "description": "This is a test, testing 4 5 6", 
      "package_type" : <package_type>, 
      "duration": 30, 
      "start_date": "2012-03-01T08:00:00Z"
    }
  }
  """
  And I have the URI for a new: package made in: OR from the request body
  When I make a: GET request to the URI for: my new package with no query string
 Then the JSON at "package" should include:
  """
  {
    "name": "anothertestpackage", 
    "description": "This is a test, testing 4 5 6", 
    "package_type" : <package_type>, 
    "duration": 30, 
    "start_date": "2012-03-01T08:00:00Z"
  }
  """

  Examples:
    | package_type |
    | "IMPRESSIONS" |
    | "CLICKS" |
    | "LEADS" |

And the contents of last_json are like this at the point the Then step is executed

{
  "package": {
    "status": "NEW",
    "account": {
      "resource_uri": "/api/v0001/accounts/fecdbb85a3584ca59820a321c3c2767d"
    },
    "name": "anothertestpackage",
    "package_type": "IMPRESSIONS",
    "margin_goal": "0.5000",
    "duration": 30,
    "resource_uri": "/api/v0001/packages/fecdbb85a3584ca59820a321c3c2767d/feea333776c9454c92edab8e73628cbd",
    "start_date": "2012-03-01T08:00:00Z",
    "description": "This is a test, testing 4 5 6"
  }
}

I should think the step would pass, but I'm getting this error instead

  Expected included JSON at path "package" (RSpec::Expectations::ExpectationNotMetError)
  features\OR\API\OR_API_Packages.feature:70:in `Then the JSON at "package" should include:'

Is this user error? should I be using a different means to determine if the expected key:value pairs are present in the JSON returned by the API? I don't really see any examples of doing this kind of comparison in your feature files for the gem, so it is difficult to know if this is not what include was intended for.

laserlemon commented 12 years ago

Include was intended more for simple value inclusion, mostly in arrays. If an API index response returned an array of objects, you could assert that the array includes one whole, identical object. Check out the matcher specs for examples.

For what you're doing, I'd break it out into separate steps:

Then the JSON at "package/name" should be "anothertestpackage"
And the JSON at "package/description" should be "This is a test, testing 4 5 6"
And the JSON at "package/package_type" should be <package_type>
And the JSON at "package/duration" should be 30
And the JSON at "package/start_date" should be "2012-03-01T08:00:00Z"

I hope that helps! Thanks for the question.

Chuckv commented 12 years ago

Yes it helps, sorry to use a 'bug' as a venue for asking questions. Is there a better place such as SO for this? if for example I was to make a json_spec tag on SO, would you folks subscribe to new postings with that tag?

I take it I could also do the above using the format shown in the first example under table format in the readme? that seems a bit cleaner

laserlemon commented 12 years ago

Oh yeah! I forgot about the table step. That would be perfect.

And no, there's no great place for questions at the moment. I could certainly subscribe to a json_spec tag and get to questions as I'm able.

Chuckv commented 12 years ago

That would be great. I'm a huge fan of using StackOverflow for this kind of thing, we're using it for a lot of the support questions on Watir and Watir-Webdriver and since it allows good formatting of code, and revising updating both questions and answers (and comments on each) we find it a great venue for such things.

OK I created the tag and seeded it with the question above and with your answer. If you and/or others are subscribed to it then you could always add something about 'getting help' to the readme and tell folks to use SO and the 'json-spec' tag (their system converts underscores to dashes for tag-names)