views:

401

answers:

4

My current project uses JSON as data interchange format. Both Front-end and Back-end team agree upon a JSON structure before start integrating a service. At times due to un-notified changes in JSON structure by back-end team; it breaks the front-end code.

Is there any external library that we could use to compare a mock JSON (fixture) with servers JSON response. Basically it should assert the whole JSON object and should throw an error if there is any violation in servers JSON format.

Additional info: App is built on JQuery consuming REST JSON services.

+2  A: 

I would recommend a schema for your JSON objects.

I use Kwalify but you can also use Rx if you like that syntax better.

Paul Tarjan
Declaring schema for JSON is interesting though. Here is my idea about fixtures approach; it could be used both for testing the integrity of back-end services as well as could be used for offline or pre-integration UI development.
shazmo
Don't mix those things. Use a schema to make sure you both understand the data contract. Use fixtures in the backend to do unit-testing. Mixing them will have you updating too many things, and will complicate your life.
Paul Tarjan
+2  A: 

Looks like you're trying to solve a problem from other end. Why should you as a front-end developer bother with testing back-end developer's work?

A JSON that is generated on server is better to test on server using standard approach, i.e. functional tests in xUnit. You could also look at acceptance test frameworks like FITnesse if you want to have tests and documentation wiki all in one.

If even after introducing testing on server you'll get invalid JSON it is a problem in human communication, not in tests.

nailxx
+2  A: 

I've been using QUnit: http://docs.jquery.com/QUnit recently for a lot of my JS code.

asyncTest http://docs.jquery.com/QUnit/asyncTest could be used pretty effectively to test JSON Structure.

Example:


asyncTest("Test JSON API 1", 1, function() {
    $.getJSON("http://test.com/json", function(data) {
        equals(data.expected, "what you expected", "Found it");
    });
});
William
A: 

Since there is no answer I'll put my two cents in.

If the problem is that you are dealing with shifting requirements from the back-end then what you need to do is isolate yourself from those changes. Put an abstraction between the front-end and back-end.

Maybe you can call this abstraction the JSON Data Format Interchange.

So when GUI unit-testing (hopefully you are TDDing your Web GUI) you will have a mock for the JSON DIF. SO when the time to integrate the back-end with the front-end*, any software changes will be done in the abstraction layer implementation. And of course you already have tests for those based upon the agreed upon JSON structure.

OBTW, I think that the server-side team should have responsibility for specifying the protocol to be used against the server.

*Why does this remind of the joke my butt and your face could be twins.

Gutzofter