in

Telligenti

Serving up fresh ideas every day, Telligent style

This Blog

Syndication

Dan Hounshell

June 2008 - Posts

  • Upgrading Kigg Unit Tests to MVC ASP.NET Preview 3

    Yesterday I posted about updating Kigg to Preview 3. Immediately after publishing that post I realized that other than doing a find-and-replace of some of the code in the unit tests I had not tried running them, I assumed they would work. Bad assumption, lesson learned.

    While upgrading the code for the Kigg web site was fairly simple, upgrading the unit tests to get all passing green lights was not as easy. The biggest issue was that in Preview 2 the return type of the Contoller methods were void, whereas in Preview 3 they are ActionResult. The unit tests had to be refactored to use that ActionResult. Below is an example of before and after:

    Before - based on Preview 2

    controller.Category(null, null); Assert.AreEqual(viewEngine.ViewContext.ViewName, "Category");Assert.IsInstanceOfType(typeof(StoryListByCategoryData), viewEngine.ViewContext.ViewData);Assert.AreEqual(((StoryListByCategoryData)viewEngine.ViewContext.ViewData.Model).Category, "All");Assert.AreEqual(((StoryListByCategoryData)viewEngine.ViewContext.ViewData.Model).PageCount, 200);Assert.AreEqual(((StoryListByCategoryData)viewEngine.ViewContext.ViewData.Model).CurrentPage, 1);

    After - based on Preview 3

    ViewResult viewResult = (ViewResult)controller.Category(null, null); Assert.AreEqual(viewResult.ViewName, "Category");Assert.IsInstanceOfType(typeof(StoryListByCategoryData), viewResult.ViewData.Model);Assert.AreEqual(((StoryListByCategoryData)viewResult.ViewData.Model).Category, "All");Assert.AreEqual(((StoryListByCategoryData)viewResult.ViewData.Model).PageCount, 200);Assert.AreEqual(((StoryListByCategoryData)viewResult.ViewData.Model).CurrentPage, 1);

    You'll see that I cast the return type as ViewResult in this case, in three cases (discussed more below) it should be RedirectToRouteResult.

    The other major change, which took me a while to figure out, is in the three tests that previously expected a redirect. Those three tests need to be changed from expecting the redirect to expecting a RedirectToRouteResult with the proper values. Below is an example of before and after:

    Before - based on Preview 2

    using (mocks.Record()){ mocks.MockControllerContext(controller); Expect.Call(delegate { controller.HttpContext.Response.Redirect(string.Empty); }).IgnoreArguments();}using (mocks.Playback()){ Assert.IsNull(viewEngine.ViewContext);}

    After - based on Preview 3

    using (mocks.Record()){ mocks.MockControllerContext(controller); //Expect.Call(delegate { controller.HttpContext.Response.Redirect(string.Empty); }).IgnoreArguments();}using (mocks.Playback()){ RedirectToRouteResult actionResult = (RedirectToRouteResult)controller.Tag(null, null); Assert.IsNotNull(actionResult); Assert.AreEqual("category", actionResult.Values["action"].ToString().ToLower());}

    For now I just commented out the call to expect the HTTP Redirect in the mock setup. The change is that the return value is now checked for NotNull rather than the context being checked for IsNull before and then the "action" value is checked to make sure it is "category". All three unit tests about redirects are expecting a redirect to the category action so the code is basically the same for all three.

    BTW, I used the NUnit tests project, but I'm sure updates to the VSTest project would be similar if not the same.

    Hopefully, I've now corrected my foul. I know I'm a lot happier now that I see all green when I run my tests. :)

  • Upgrading Kigg to ASP.NET MVC Preview 3

    I downloaded Kigg from CodePlex, a Digg like application written with ASP.NET MVC, a few weeks ago and began playing around with it this evening. The current source code is setup for the MIX08 Preview 2 version not the current version, Preview 3. I have Preview 3 installed and did not want to go back so I set about updating it. The process went pretty well considering this was my first time really doing anything with the MVC framework.

    I plan on submitting a patch of the needed updates once I can actually connect to the source code repo (I'm able to do so with other projects on CodePlex, like SVNBridge, but I am having trouble specifically with Kigg). But in the meantime if you'd like to upgrade Kigg to Preview 3 simply follow the steps in the Preview 3 Readme Release Notes in the section "Upgrading an Existing Preview2 Application to Preview 3" and it will work fine. Those instructions along with some help from Intellisense and the C# compiler are all that you'll need to do so.

  • LINQ to SQL - How to "Where in (value1,value2, ... valueN)"

    According to FeedBurner the count of my blog subscribers has been steadily decreasing the last couple of weeks. I can't blame those quitters because my posts have been few and far between. But for those of you beautiful and intelligent people with enough patience to wait for something worthwhile - you're in luck today.

    Several days ago I was working with a LINQ to SQL statement where I wanted to get an item if it's name existed in a string array of passed in values. I needed a LINQ translation for the SQL " where in (value1, value2, ... , valueN)" syntax. Intellisense provided little value. I had to turn to Google for answers. Do you realize how difficult it is to search for the keyword "in" on Google?!? It was nightmare. I think I got lucky when I tried "where in value". I found this post from Dan Wahlin that provided an example of exactly what I was trying to do:

    http://weblogs.asp.net/dwahlin/archive/2008/05/09/using-linq-to-perform-quot-where-in-value1-value2-quot-queries.aspx

    The solution is actually rather simple if you look at it from a programming perspective, but I have a tough time remembering that LINQ is not a SQL syntax, but is instead a first class part of the framework/language that is used for querying. Below is the example that Dan provided in his post:

    public static Product[] GetProducts(Guid[] prodIDs) { return GetProducts().Where(p => prodIDs.Contains(p.ProductID)).ToArray<Product>(); }

    Just like you can say "if (myobj == null)" and it means the same thing as "if (null == myobj)" the LINQ syntax is a bit clearer if you think about reversing the order of the equality. So instead of thinking about it as "if my object is in this bucket" think of it instead as "if this bucket contains my object". After seeing this it made sense to me. And LINQ and I became closer, maybe even friends.

  • Things I learned this weekend

    Wield Chkdsk sparingly

    Do not set large hard drives (and especially not two large hard drives) to run an error check (chkdsk) on next reboot if:

    • your PC only supports USB keyboards and
    • your PC's bios does not support "Legacy USB settings" and
    • you've never installed the Recovery Console to your PC.

    You'll have no way to cancel the chkdsk and you'll have to wait several (like 12+) hours for it to complete. No, regular Safe Mode doesn't help, because it still tries to run chkdsk before booting the OS.

     

    LINQ to SQL is gooood!

    • LINQ to SQL creates SQL just as good as I would write by hand, and most of the times better, and sometimes much better.
    • LINQPad = For The WIN !!!
    • To gain a better understanding of LINQ to SQL use LINQPad to see what the query syntax looks like as Lambda syntax and SQL Profiler (and/or LINQPad) to see the SQL being generated.
    • You can pass IQueryable around as many times as you want and the SQL is never executed until its turned into a list (think used as a datasource even if ToList() is not specifically called). I had a tough time wrapping my head around this one - SQL Profiler helped prove it to me.
    • The new ListView + the new DataPager + LINQ = beautiful music
    • Between LINQ to SQL, Subsonic, and Castle ActiveRecord I may never hand-code another line of SQL ever again.

     

    Miscellaneous

    • The installation of VS 2008 to my workstation was nowhere near as difficult as I'd feared it to be. As far as I can tell it didn't muck anything up.
    • GraphJam is hilarious
    • a Sunday afternoon nap is not a blessing, it is a right !
Powered by Community Server (Commercial Edition), by Telligent Systems