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. :)