OK – So I started out like this,
public ViewResult Index()
{
return View(service.GetProjects());
}
And here is my test.
[TestMethod]
public void Index_Will_Return_A_List_Of_Active_Projects()
{
var view = controller.Index();
Assert.AreEqual(view.ViewData.Model.GetType(), typeof(List<Project>));
}
All that is rocken with dokken but then I added login and if the user isn’t authenticated I redirect them to the login page. Here is what the new method looks like.
public ActionResult Index()
{
if (Request.IsAuthenticated)
return View(service.GetProjects());
return RedirectToAction("Login", "Account");
}
My problem is this – I can’t figure out how to fix the unit test for the life of me. I can’t return a ViewResult anymore so I can’t check the .ViewData.Model property but I can’t figure out how to redirect while still returning a viewresult. I have been trolling the site and I found this How do I redirect within a ViewResult or ActionResult function? but that doesn’t really help.
If someone can tell me what to here that would rule – I am stumped.
I would remove that logic from your action and use the AuthorizeAttribute instead. Then your test doesn’t change and you can create a seperate test that asserts the action is being decorated by the attribute.