I am learning ASP MVC , thus new to TDD and MVC frameworks. I find an issue while writing mock TestMethod for testing a Action inside the Controller, that ” RedirectToAction” in its body.
Writing the testMethod to make sure the redirected Action is correct. I read it’s possible to check with RedirectToRouteResult.RouteValues, and getting value for “action” key. Even in debug (watching down to the properties) I am able to get it correct. But @ compile time the test is showing exception Cannot apply Indexing with [] to an expression of type RedirectToRouteResult.RouteValues.
Following is the code snippet for the reference:
public void TestHomeControllerContactRedirectRoute()
{
HomeController controller = new HomeController();
ContactMessage message=new ContactMessage(){
Email="abc@xyz.com",
MessageBody="Some message here!!",
Name="Sun"
};
RedirectToRouteResult resultRoute = controller.Contact(message) as RedirectToRouteResult;
if (resultRoute != null)
{
Assert.AreEqual("Contact", resultRoute.RouteValues["action"]);
}
}
Potentially missing a reference to System.Web
Source: http://samritchie.net/2011/01/06/mvc-unit-testing-error/