How to approve / test JSON server responses?

My current project uses JSON as a data exchange format. Both the front and rear teams agree to a JSON structure before starting service integration. From time to time due to unpublished changes to the JSON structure by the background command; It violates the external code.

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

Additional Information: The application is built on jQuery, consuming REST JSON services.

+6
json javascript jquery unit-testing integration-testing
source share
5 answers

I would recommend a schema for your JSON objects.

I use Kwalify , but you can also use Rx if you like this syntax.

+6
source share

I used QUnit: http://docs.jquery.com/QUnit recently for a lot of my JS code.

asyncTest http://docs.jquery.com/QUnit/asyncTest could be used quite effectively to test the 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"); }); }); 
+5
source share

It looks like you are trying to solve the problem from the other end. Why do you, as a front-end developer, have problems testing developer support?

JSON created on the server is best tested on the server using a standard approach, i.e. functional tests in xUnit. You can also see acceptance testing frameworks such as FITnesse if you want to have tests and wiki documentation all in one.

If even after implementing testing on the server you get invalid JSON, this is a problem in human communication, not in tests.

+3
source share

Since there is no answer, I will put my two cents.

If the problem is that you are dealing with transferring requirements from a database, then what you need to do is isolate yourself from these changes. Put an abstraction between the interface and the end.

Perhaps you can call this abstraction JSON format data exchange.

So, when the GUI is testing (I hope you are TDDing your web GUI), you will have a layout for JSON DIF. SO, when it is time to integrate with the interface * , any software changes will be made in the implementation of the abstraction layer. And of course, you already have tests for those based on a consistent JSON structure.

OBTW, I think that the server team should be responsible for specifying the protocol that will be used against the server.

* Why does this remind you of a joke with my butt, and your face may be a twin.

0
source share

https://github.com/skyscreamer/JSONassert can help eliminate false positives, so if the order of the fields returned by the server changes, but the general answer is the same, it does not crash.

0
source share

All Articles