* 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…)

So, I’ve struggled some with this and did not have much help with searching.

Here is the problem.

You have a declaration that looks like this:

   1: public partial class CompanyQuery   {
   2:       public List<int> CompanyTypeIds { get; set; }
   3: }

Then, you need to figure out if CompanyTypeIds is populated using reflection.

(more…)

So, I asked the following question in at http://forums.asp.net/t/1408631.aspx

image

The problem is, I’ve got 30 methods that all have the same signature, but have different implementations.  Mike Banavige (the lead moderator at the forums) suggested I look at the following article on reflection:  http://www.csharphelp.com/archives/archive200.html.  Turns out, that helped my solve my problem exactly.  Rather than go through a lengthy line by line explanation, I’m going to post some of my before and after code.  It’s pretty clear what is happening, but I thought it would be nice to show a real example.

(more…)

© 2005 PeterKellner.net. All Rights Reserved