* I’m adding this comment after the post has been written to let anyone coming here know of another great post that goes further to explain no only what I’ve done here, but also two other ways including Mock and using Json Serialization. The Post is written by Ashic Mahtab, aka “HeartattacK” on the forums. His well written and informative article is here: ASP.NET MVC – Unit Testing JsonResult Returning Anonymous Types.
This post will show how to return a simple Json result from an ASP.NET MVC 2.0 web project. It will show how to test that result inside a unit test and essentially pick apart the Json, just like a JavaScript (or other client) would do. It seems like it should be very simple (and indeed, once you see the answer it is), however there are lots of length discussions on the forums about this with all kinds of positives and negatives. The one I based my solution on is from Stack Overflow and is here.. My personal length discussion that did not really yield a satisfactory answer is here.
If you follow my method, you’ll be able to unit test a JsonResult created by an MVC asp.net web application.
(more…)







