small medium large xlarge

Generic-user-small
15 Feb 2012, 00:53
Chuck van der Linden (11 posts)

Firstly, found a great gem to assist with doing REST testing if you are handling JSON. But need a little help as to the best way to get it wired in.

The gem probably arrived too late for the book considering it only came out last july and this might be the first you are hearing of it, but based on a first glance, the JSON_Spec gem (see here: https://github.com/collectiveidea/json_spec and blog post introducing it here: http://collectiveidea.com/blog/archives/2011/07/12/test-your-api-with-cucumber-and-json_spec/ ) might be something well worth considering mentioning.

It adds a bunch of awsome very ‘json aware’ cucumber steps such as

Then the JSON should include {“key”:”value”} Then the JSON at “path” should include {“key”:”value”} Then the JSON should include [“entry”,”entry”] Then the JSON at “path” should include [“entry”,”entry”] Then the JSON should include “string” Then the JSON at “path” should include “string” Then the JSON should include 10 Then the JSON at “path” should include 10 Then the JSON should include 10.0 Then the JSON at “path” should include 10.0 Then the JSON should include 1e+1 Then the JSON at “path” should include 1e+1 Then the JSON should include true Then the JSON at “path” should include true Then the JSON should include false Then the JSON at “path” should include false Then the JSON should include null Then the JSON at “path” should include null

To work the gem needs you to define a ‘last_json’ method that will return the body from the request. They say

If you're using Capybara, it could be as simple as:

def last_json
  page.source
end

However, how would I get it working with something like HTTP-Party or the Rest-Client gem I am using? both return a ‘response’ which has methods like .body, .header, etc.. in your example (and my current code) you are passing this around via a @last_response instance variable.

My ‘gut’ tells me that the right way to do this is make some kind of ‘last_response’ object that the world knows about, and then you could just define a helper method for last_json that simply returned last_response.body.

But how do I (who am still pretty ‘newbie’ when it comes to ruby) figure out how to create a new one of those objects (for code I don’t know) so I can add it to my world extensions, and then also create a proper helper method that makes use of it (as well as do away with the instance variable in the rest of my step code.

Avatar_pragsmall
15 Feb 2012, 11:04
Matt Wynne (92 posts)

You’re on the right lines Chuck.

Create a file in features/support called, say json_spec.rb, then in there do this:

module JSONSpecInterface
  def last_json
    @last_response.body
  end
end

World(JSONSpecInterface)

That should do it.

Generic-user-small
15 Feb 2012, 19:21
Chuck van der Linden (11 posts)

OK that was easier than I expected.. somehow I expected some kind of scope issue with using the instance variable inside a def, but maybe that is part of the magic of ‘world’ that I am still coming to understand.

BTW: one of the great things I forgot to mention about that gem is that it can be configured to ignore specific fields that would be nearly impossible to predict such as ‘id”, “created_at” etc. That makes it easy to do equivalency compares which automatically exclude some fields

Now I just have to figure out why the step I’m trying to use is not working, but that’s not your problem.

You must be logged in to comment