How to unit test this simple ASP.NET MVC controller
- by Frank Schwieterman
Lets say I have a simple controller for ASP.NET MVC I want to test. I want to test that a controller action (Foo, in this case) simply returns a link to another action (Bar, in this case).
How would you test this? (either the first or second link)
My implementation has the same link twice. One passes the url throw ViewData[]. This seems more testable to me, as I can check the ViewData collection returned from Foo(). Even this way though, I don't know how to validate the url itself without making dependencies on routing.
The controller:
public class TestController : Controller
{
public ActionResult Foo()
{
ViewData["Link2"] = Url.Action("Bar");
return View("Foo");
}
public ActionResult Bar()
{
return View("Bar");
}
}
the "Foo" view:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/Views/Shared/Site.Master"%>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<%= Html.ActionLink("link 1", "Bar") %>
<a href="<%= ViewData["Link2"]%>">link 2</a>
</asp:Content>