DynamicJson and unit-testing JSON services

I’m currently messing with setting up unittests for a JSON/REST’ish service. My goal is having (or at least have a looke at having :)) a test suite that executes over HTTP in exatcly the same manner as an actual client. One advantages of this I think, will be the ability to execute the tests against a remote system, once the system is deployed.

Trying to figure out the best way to inspect the returned JSON data, I came across DynamicJson (http://dynamicjson.codeplex.com/). The library combines the new C# 4.0 dynamic features with a JSON parser to allow for what you see below:

[TestMethod]
public void GetList()
{
    var webClient = new WebClient();
    webClient.Encoding = UTF8Encoding.UTF8;
    webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

    var result = DynamicJson.Parse(webClient.DownloadString("http://localhost/WebSite/List/BE33A625-63F3-4014-85E7-D306F00A5A7E"));

    Assert.AreEqual("TestList", result.Title);
}

The library does contain a serializer as well, but it only support basic types (so GUID’s for instance are not supported). I find the JavaScriptSerializer much more usefull..

Advertisements

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: