1

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

I am using a combination of Cucumber, along with the rest-client, and json_spec gems to create a test suite for a restful API. The approach is similar to that given in the Cucumber Book Note that in this case, since the 'customer' is a developer, the 'language of the business' is far more technical than you would normally express in cucumber scenarios

I am having a problem with the json_spec cucumber step "Then the JSON at "path" should include:"

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:'

It is unclear what that error is telling me in terms of what is wrong. 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.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
  • Can you show us the step definition for this step please? – Ryan Bigg Feb 15 '12 at 20:55
  • @RyanBigg It's defined automatically as a part of the json_spec gem. I didn't have to write any code for that step. Apparently it was designed to find far more 'fragmentary' bits of data within the json than what I was using it for. Applying a different step from their library fixed the issue. I can tell this thing is going to save me oodles of time that would have been spent in tons of json parsing and such to do comparisons of fragments of json.. if you are using cucumber for API testing I would strongly encourage you check this thing out! – Chuck van der Linden Feb 15 '12 at 21:06

1 Answers1

1

Heh just got an answer from one of the gem authors via another venue. Will post it here

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"

Or you could make use of a table format to make that more succinct such as

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

I hope that helps! Thanks for the question.

Indeed it did help very much, thank you 'laserlemon'

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43