最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to assertunit-test servers JSON response? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Mar 24, 2010 at 3:15 Jon Limjap 95.4k15 gold badges103 silver badges153 bronze badges asked Mar 24, 2010 at 3:01 shazmohshazmoh 1,9461 gold badge13 silver badges15 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

I would recommend a schema for your JSON objects.

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

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");
    });
});

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.

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.

https://github.com/skyscreamer/JSONassert may be helpful in eliminating false positives, so that if the order of fields returned by server changes, but overall response is the same, it doesn't trigger a failure.

发布评论

评论列表(0)

  1. 暂无评论